matplotlib使用方法

本文以化坐标图和条形图为例,说明matplotlib的使用方法

eg1:

import numpy as np 
from matplotlib import pyplot as plt

x = np.arange(0,20,2)

plt.axis([0,18,0,250])                   #设置x轴y轴的刻度范围
plt.grid(True)                          #设置图标有网格
plt.xlabel('x axis')                     #设置X轴的名称
plt.ylabel('y axis')
plt.xticks(x)                            #设置X轴的刻度位置 
plt.yticks(np.arange(0,300,50))
plt.title('matplotlib')                  #设置图标标题
plt.plot(x,x,label='x')                  
plt.plot(x,x*x,'g--',label='x*x')       #绘图x,y,'线条的颜色及形状',label
plt.plot(x,x*5+100,label='x*5+100')
plt.bar(x,x*2+50)
plt.legend()                             #设置图标图例说明
plt.show()

eg2:

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

plt.title('Man&Woman Meanheight Grah')
plt.ylabel('Height')
plt.xlabel('People')
x = np.arange(len(labels))
width = 0.35
plt.bar(x+width/2,men_means,width,color='red',tick_label=labels,label='Man')  #显示位置,数据数值,显示宽度,颜色,x轴label
plt.bar(x-width/2,women_means,width,color='blue',label='Woman')
plt.legend()
plt.show()

eg3:

子图表的用法

x1 = ['student1','student2','student3']
y1 = [90,70,55]
x2 =['red','orange','blue']
y2 = [80,60,80]

plt.subplot(2,1,1)          #设置一个2行1列的第一个子图
plt.yticks(np.arange(0,120,20))
plt.bar(x1,y1)
plt.subplot(2,1,2)
plt.bar(x2,y2)
plt.show()

猜你喜欢

转载自www.cnblogs.com/smarttony/p/12292586.html