# coding: utf-8 import matplotlib.pyplot as plt import numpy as np # * 散点图 #plot()该函数是制作线形图 plt.plot([1,2,5,4,2,6,4]) plt.show() # * subplot()在同一个平面上,创建多个子图 x = [1,2,3,4] y = [5,4,7,2] #创建一个figure图形对象,可以将多个子图同时放在一个figure对象上 plt.figure() #参数:1.表示figure对象上可以放置5行子图 # 2.表示figure对象上可以放置3列子图 # 3.表示子图的绘制位置,按照从左到右,从上到下排列的 plt.subplot(5,3,1) plt.plot(x,y) plt.subplot(5,3,2) plt.plot(x,y) plt.subplot(5,3,3) plt.plot(x,y) #折线图 y1 = [7,8,5,3] plt.subplot(5,3,4) plt.bar(x,y) plt.subplot(5,3,5) plt.barh(x,y) #横直方图 y1 = [7,8,5,3] plt.subplot(5,3,6) plt.bar(x,y1,color='r') y1 = [7,8,5,3] plt.subplot(5,3,7) plt.bar(x,y1,color='y',bottom=y1) #直方图 y1 = [7,8,5,3] plt.subplot(5.,3,8) plt.boxplot(x,y1) #箱型图 y1 = [7,8,5,3] plt.subplot(5,3,9) plt.scatter(x,y1) #散点图 plt.show() x = np.arange(100) fig = plt.figure() # 添加三个子图,给三个空白图片绘制子图内容 ax1 = fig.add_subplot(2,2,1) plt.plot(x,x) ax2 = fig.add_subplot(2,2,2) plt.plot(x,x**2) ax3 = fig.add_subplot(2,2,3) plt.plot(np.random.randn(10),'k--') # k:表示颜色 ax4 = fig.add_subplot(2,2,4) # bins:调整柱子的宽度,值越大柱子越窄; # alpha:柱子的透明度,取值范围0-1,0完全透明,1非透明,默认值为1 plt.hist(np.random.randn(10),bins=20,color='y',alpha=0.5) plt.show() # * subplots() 可以快速生成一个n行n列的空白图标,相当于是add_subplot()的 一个快捷方式 fig,axes = plt.subplots(2,3) axes ax1 = axes[1][1] ax1.bar([1,2,3,4],[1,5,3,4]) plt.show() # ## 图片的基本设置 # *颜色的设置 # *数据标记 # *线性的设置 x = [1,2,3,4] y = [5,3,2,4] plt.figure() # r线段为红色 # o数据标记 # --线段类型 plt.plot(x,y,'ro--') #plot() hist()如果只指定了一个值,默认是y轴的数据 plt.plot(np.random.randn(30),color='y',linestyle='dashed', marker='o',drawstyle='steps-post',label='test') plt.legend() #显示图例 plt.show() fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(np.random.randn(100),label='People') # 设置图标标题 ax.set_title('my custom plot') # 设置x轴或者y轴上的刻度 # 针对某一个图标 # ax.set_xlim([0,100]) # ax.set_ylim([-4,4]) # 针对全局的图标 # plt.xlim([0,150]) # 设置x轴或者y轴只显示哪些刻度 ax.set_xticks([0.,100,200]) # plt.xticks([0,200]) #设置刻度标签, ax.set_yticks([-2,-1,0]) ax.set_yticklabels(['one','two','three'],rotation=30,fontsize='larger') # rotation:标签旋转角度,fontsize:标签字体大小 # 设置坐标轴的标签 ax.set_xlabel('number') ax.set_ylabel('num') ax.legend(loc='best') plt.show()