如何用 x 轴绘制两个数组的图形将是两个数组的长度?
How to plot a graph out of two arrays with the x axis will be the length of the two arrays?
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,)
4 只是一个整数。你需要的是1-4
的列表
plt.plot(range(1,5), array1, 'g', label='Label 1')
plt.plot(range(1,5), array2, 'b', label='Label 2')
输出:
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,)
4 只是一个整数。你需要的是1-4
的列表plt.plot(range(1,5), array1, 'g', label='Label 1')
plt.plot(range(1,5), array2, 'b', label='Label 2')
输出: