if you write for i in test_file
what happens is that python iterates through the array test_file
and in the first iteration takes the first item [2000, 3.8]
and assigns i
to this. This is not what you want. You should change your code into something like this:
from matplotlib.pyplot import plot, axis, show
test_file = [[2000, 3.8], [2001, -2.4], [2002, 5.8], [2003, -10.5], [2004, 2.1], [2005, 2.1], [2006, 6.9], [2007, -3.9]]
for year, value in test_file:
plot(year, value, marker="o", color="blue", markeredgecolor="black")
axis([2000, 2007, -12, 10])
show()
there is no line connecting the dots
In this case you need to reform your data a bit:
dates = [i[0] for i in test_file]
values = [i[1] for i in test_file]
plot(dates, values, '-o', color="blue", markeredgecolor="black")
x-axis is 1-7 vs. 2000 - 2007
Yes, this is a little bit odd. One way to fix this is to turn the years which are now stored as integers into python datetime.date
and run autofmt_xdate()
on them, so the whole code reads:
from matplotlib.pyplot import plot_date, axis, show, gcf
import datetime
test_file = [[2000, 3.8], [2001, -2.4], [2002, 5.8], [2003, -10.5], [2004, 2.1], [2005, 2.1], [2006, 6.9], [2007, -3.9]]
dates = [datetime.date(i[0], 1, 1) for i in test_file]
values = [i[1] for i in test_file]
plot_date(dates, values, "-o", color="blue", markeredgecolor="black")
gcf().autofmt_xdate()
show()