0
votes

Trying to create a simple graph using a list of lists with year and value:

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 i in test_file:
    plot(test_file[i][0], test_file[i][1], marker="o",
         color="blue", markeredgecolor="black")
    axis([2000, 2007, -12, 10])
    show()

But I'm getting this error: TypeError: list indices must be integers or slices, not list

Thanks.

3

3 Answers

1
votes

If you don't mind importing numpy, converting the list to an array works pretty painlessly:

import numpy as np
import matplotlib.pyplot as plt

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]]
test_file = np.array(test_file)

plt.plot(test_file[:, 0], test_file[:, 1])
plt.show()
0
votes

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()
0
votes

For line connecting dots, you need to specify plot data together in a list as below.

Bonus: I added x , y low and high value as variables instead of hardcoded in case data in test_file changes.

EDIT
Added ScalerFormatter to the code for y-axis to show actual y-axis values.

import matplotlib.pyplot as plt
from  matplotlib import ticker
import math

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]]
year   = [ i[0] for i in test_file]
value  = [ i[1] for i in test_file]
x_low  = min (year)
x_high = max(year)

y_low = float(math.floor(min(value)))
y_high = float(math.ceil(max(value)))

plt.xlabel('Year')
plt.ylabel('Value')
plt.title('Year and Value')


plt.plot(year ,value, marker="o", color="black", markeredgecolor="black")
plt.axis([x_low , x_high, y_low, y_high])

fmt=ticker.ScalarFormatter(useOffset=False)
fmt.set_scientific(False)
ax=plt.gca()
ax.xaxis.set_major_formatter(fmt)

plt.show()

Default Plot

Default Plot

Plot with Actual Y-Axis Data Formatted Plot