首页 > 解决方案 > 如何用 x 轴从两个数组中绘制图形将是两个数组的长度?

问题描述

array1=[1,2,3,4]
array2=[5,6,7,8]

plt.plot(4, array1, 'g', label='Label 1')
plt.plot(4, array2, 'b', label='Label 2')
plt.title('Sample Graph')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()

但是当我运行它时它说

ValueError: x and y must have same first dimension, but have shapes (1,) and (4,)

标签: pythonmatplotlibplotgraphdata-science

解决方案


4 只是一个整数。你需要的是一个 1-4 的列表

plt.plot(range(1,5), array1, 'g', label='Label 1')
plt.plot(range(1,5), array2, 'b', label='Label 2')

输出:

输出


推荐阅读