[英]Plot curve using pyplot
I have 2 curves with 5 data points: 我有2条曲线和5个数据点:
a = np.array([['16x28', 0.023392, 0.006976],
['64x36', 0.029888, 0.039104],
['512x288', 0.033328, 2.198592],
['1024x576', 0.065632, 5.864992],
['3840x2160', 0.801120, 76.550461]])
when I plot it, I expect 2 curves but get straight lines instead: 当我绘制它时,我希望有2条曲线,但会得到直线:
plt.xticks([0,1,2,3, 4],a[:,0])
plt.plot(a[:,1])
plt.plot(a[:,2])
How can I make pyplot plot curves like it should be? 如何使pyplot绘图曲线像它应该的那样?
UPDATE: I use Python 3.5.2 and matplotlib 2.1.1 更新:我使用Python 3.5.2和matplotlib 2.1.1
Matplotlib < 2.1 does not allow the plotting of strings directly, however matplotlib 2.1 does. Matplotlib <2.1不允许直接绘制字符串,但是matplotlib 2.1可以。 If you examine your data you will see that they are strings:
如果检查数据,您将看到它们是字符串:
print (a[1,1], type(a[1,1]))
# 0.029888 <class 'numpy.str_'>
Therefore, you need to convert your data to floats. 因此,您需要将数据转换为浮点数。 You can do this using
numpy.ndarray.astype
. 您可以使用
numpy.ndarray.astype
进行此操作。
import matplotlib.pyplot as plt
import numpy as np
a = np.array([['16x28', 0.023392, 0.006976],
['64x36', 0.029888, 0.039104],
['512x288', 0.033328, 2.198592],
['1024x576', 0.065632, 5.864992],
['3840x2160', 0.801120, 76.550461]])
plt.xticks([0,1,2,3,4], a[:,0])
plt.plot(a[:,1].astype(float))
plt.plot(a[:,2].astype(float))
plt.show()
Giving: 赠送:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.