1.绘制折线图
import matplotlib.pyplot as plt import numpy as np %matplotlib inline x = np.arange(9) y = np.sin(x) z = np.cos(x) # marker数据点样式,linewidth线宽,linestyle线型样式, #color表示颜色 plt.plot(x, y, marker='*', linewidth=1, linestyle='--', color='orange') plt.plot(x, z) plt.title('matplotlib') plt.xlabel('height',fontsize=15) plt.ylabel('width',fontsize=15) # 设置图例 plt.legend(['Y','Z'], loc='upper right') plt.grid(True) plt.show()
结果图:
2.绘制散点图
示例1:
fig,ax = plt.subplots() plt.rcParams['font.family'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False x1 = np.arange(1,30) y1 = np.sin(x1) ax1 = plt.subplot(1,1,1) plt.title('散点图') plt.xlabel('X') plt.ylabel('Y') lvalue = x1 ax1.scatter(x1,y1,c='c' ,s = 100,linewidths = lvalue,marker = 'o') plt.legend('x1') plt.show()
结果图:
示例2:
fig,ax=plt.subplots() plt.rcParams['font.family']=['SimHei']#用来显示中文标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 for color in ['red','green','blue']: n=500 x,y=np.random.randn(2,n) ax.scatter(x,y,c=color,label=color,alpha=0.3,edgecolors='none') ax.legend() ax.grid(True) plt.show()
结果图:
3.绘制直方图
fig,axes = plt.subplots(2,1) data = pd.Series(np.random.randn(16),index = list('abcdefghijklmnop')) data.plot.bar(ax = axes[0],color = 'k',alpha = 0.7) #垂直柱状图 data.plot.barh(ax = axes[1],color = 'k',alpha = 0.7) #alpha设置透明度
结果图:
4.绘制饼图
plt.figure(figsize = (6,6)) #建立轴的大小 labels = 'Springs','Summer','Autumn','Winter' x = [15,30,45,10] explode = (0.05,0.05,0.05,0.1) #这个是控制分离的距离的,默认饼图不分离 plt.pie(x,labels = labels,explode = explode,startangle = 60,autopct = '%1.1f%%') #qutopct在图中显示比例值,注意值的格式 plt.title('Rany days by season') plt.tick_params(labelsize = 12) plt.show()
结果图:
5.绘制箱线图
np.random.seed(0) #设置随机种子 df = pd.DataFrame(np.random.rand(5,4), columns = ['A', 'B', 'C', 'D']) #生成0-1之间的5*4维度数据并存入4列DataFrame中 df.boxplot() #也可用plot.box() plt.show()
结果图: