1.绘制折线图 (plot())
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
#准备数据
x= np.arange(1,11)
y1=np.array([22,23,25,24,21,26,22,20,19,24])
y2=np.array([19,15,18,14,11,16,17,11,14,16])
#绘制折线图
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()
效果如下图所示。
2.绘制条形图 (bar())
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
x= np.arange(5)
y1=np.array([26,22,20,19,24])
y2=np.array([16,17,11,14,16])
bar_width=0.3#条形的宽度
#根据多组数据绘制条形图
plt.bar(x,y1,tick_label=['a','b','c','d','e'],width=bar_width,color='red')
plt.bar(x+bar_width,y2,width=bar_width,color='green')
plt.show()
效果如下图显示。