如何根据列表绘制列表列表?

cat*_*zen 0 python matplotlib valueerror

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()
Run Code Online (Sandbox Code Playgroud)

我得到一个ValueError4, 3。我知道这一点,x而且y必须是平等的。我以为len(y[0])会工作。

对于 中的每个子列表y,我想生成一行,其x值对应于2000, 2001, 2002, 2003

Bas*_*sil 5

对于简单的 Pythonic 解决方案,请执行以下操作:

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

y嵌套列表中的每一项都是您要针对的列表x,因此这种方法在这里非常有效。