Reputation: 75
x = [2000,2001,2002,2003]
y = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
for i in range(len(y[0])):
plt.plot(x,[pt[i] for pt in y])
plt.show()
I'm getting a ValueError
of 4, 3
. I know that x
and y
must be equal. I thought len(y[0])
would work.
For each of the sublists in y
, I want to generate a line with their x
values corresponding to 2000, 2001, 2002, 2003
.
Upvotes: 0
Views: 1518
Reputation: 4743
Another solution would be using pandas
package in the following way:
import pandas as pd
import matplotlib.pyplot as plt
x = [2000,2001,2002,2003]
y = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
df = pd.DataFrame(y).transpose()
df.index=x
df.plot()
plt.show()
And the result would be:
In [30]: df
Out[30]:
0 1 2
2000 1 5 9
2001 2 6 10
2002 3 7 11
2003 4 8 12
Upvotes: 2
Reputation: 704
For a simple Pythonic solution, do the following:
for y_values in y:
plt.plot(x, y_values)
plt.xticks(x) # add this or the plot api will add extra ticks
plt.show()
Each item in your y
nested list is the list you want to plot against x
, so this approach works perfectly here.
Upvotes: 5
Reputation: 175
[pt[i] for pt in y]
on i = 0
will give you [1,5,9]
.
I think you need [1,2,3,4]
, so use y[i]
instead of [pt[i] for pt in y]
.
Upvotes: 0