简体   繁体   中英

Plot curve using pyplot

I have 2 curves with 5 data points:

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:

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?

UPDATE: I use Python 3.5.2 and matplotlib 2.1.1

Matplotlib < 2.1 does not allow the plotting of strings directly, however matplotlib 2.1 does. 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 .

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM