畫(huà)圖需要導(dǎo)入一下包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
繪圖操作步驟
#使用numpy產(chǎn)生數(shù)據(jù)
x=np.arange(-5,5,0.1)
y=x*3
#創(chuàng)建窗口沙咏、子圖
#方法1:先創(chuàng)建窗口窄刘,再創(chuàng)建子圖。(一定繪制)
fig = plt.figure(num=1, figsize=(15, 8),dpi=80) #開(kāi)啟一個(gè)窗口慨绳,同時(shí)設(shè)置大小谆构,分辨率
ax1 = fig.add_subplot(2,1,1) #通過(guò)fig添加子圖署惯,參數(shù):行數(shù)行施,列數(shù),第幾個(gè)匹层。
ax2 = fig.add_subplot(2,1,2) #通過(guò)fig添加子圖隙笆,參數(shù):行數(shù),列數(shù)升筏,第幾個(gè)撑柔。
print(fig,ax1,ax2)
#方法2:一次性創(chuàng)建窗口和多個(gè)子圖。(空白不繪制)
fig,axarr = plt.subplots(4,1) #開(kāi)一個(gè)新窗口您访,并添加4個(gè)子圖铅忿,返回子圖數(shù)組
ax1 = axarr[0] #通過(guò)子圖數(shù)組獲取一個(gè)子圖
print(fig,ax1)
#方法3:一次性創(chuàng)建窗口和一個(gè)子圖。(空白不繪制)
ax1 = plt.subplot(1,1,1,facecolor='white') #開(kāi)一個(gè)新窗口灵汪,創(chuàng)建1個(gè)子圖檀训。facecolor設(shè)置背景顏色
print(ax1)
#獲取對(duì)窗口的引用,適用于上面三種方法
# fig = plt.gcf() #獲得當(dāng)前figure
# fig=ax1.figure #獲得指定子圖所屬窗口
# fig.subplots_adjust(left=0) #設(shè)置窗口左內(nèi)邊距為0享言,即左邊留白為0峻凫。
#設(shè)置子圖的基本元素
ax1.set_title('python-drawing') #設(shè)置圖體,plt.title
ax1.set_xlabel('x-name') #設(shè)置x軸名稱,plt.xlabel
ax1.set_ylabel('y-name') #設(shè)置y軸名稱,plt.ylabel
plt.axis([-6,6,-10,10]) #設(shè)置橫縱坐標(biāo)軸范圍览露,這個(gè)在子圖中被分解為下面兩個(gè)函數(shù)
ax1.set_xlim(-5,5) #設(shè)置橫軸范圍荧琼,會(huì)覆蓋上面的橫坐標(biāo),plt.xlim
ax1.set_ylim(-10,10) #設(shè)置縱軸范圍,會(huì)覆蓋上面的縱坐標(biāo),plt.ylim
xmajorLocator = MultipleLocator(2) #定義橫向主刻度標(biāo)簽的刻度差為2的倍數(shù)。就是隔幾個(gè)刻度才顯示一個(gè)標(biāo)簽文本
ymajorLocator = MultipleLocator(3) #定義縱向主刻度標(biāo)簽的刻度差為3的倍數(shù)命锄。就是隔幾個(gè)刻度才顯示一個(gè)標(biāo)簽文本
ax1.xaxis.set_major_locator(xmajorLocator) #x軸 應(yīng)用定義的橫向主刻度格式堰乔。如果不應(yīng)用將采用默認(rèn)刻度格式
ax1.yaxis.set_major_locator(ymajorLocator) #y軸 應(yīng)用定義的縱向主刻度格式。如果不應(yīng)用將采用默認(rèn)刻度格式
ax1.xaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用定義的主刻度格式
ax1.yaxis.grid(True, which='major') #x坐標(biāo)軸的網(wǎng)格使用定義的主刻度格式
ax1.set_xticks([]) #去除坐標(biāo)軸刻度
ax1.set_xticks((-5,-3,-1,1,3,5)) #設(shè)置坐標(biāo)軸刻度
ax1.set_xticklabels(labels=['x1','x2','x3','x4','x5'],rotation=-30,fontsize='small') #設(shè)置刻度的顯示文本累舷,rotation旋轉(zhuǎn)角度浩考,fontsize字體大小
plot1=ax1.plot(x,y,marker='o',color='g',label='legend1') #點(diǎn)圖:marker圖標(biāo)
plot2=ax1.plot(x,y,linestyle='--',alpha=0.5,color='r',label='legend2') #線圖:linestyle線性夹孔,alpha透明度被盈,color顏色,label圖例文本
ax1.legend(loc='upper left') #顯示圖例,plt.legend()
ax1.text(2.8, 7, r'y=3*x') #指定位置顯示文字,plt.text()
ax1.annotate('important point', xy=(2, 6), xytext=(3, 1.5), #添加標(biāo)注搭伤,參數(shù):注釋文本只怎、指向點(diǎn)、文字位置怜俐、箭頭屬性
arrowprops=dict(facecolor='black', shrink=0.05),
)
#顯示網(wǎng)格身堡。which參數(shù)的值為major(只繪制大刻度)、minor(只繪制小刻度)拍鲤、both贴谎,默認(rèn)值為major。axis為'x','y','both'
ax1.grid(b=True,which='major',axis='both',alpha= 0.5,color='skyblue',linestyle='--',linewidth=2)
axes1 = plt.axes([.2, .3, .1, .1], facecolor='y') #在當(dāng)前窗口添加一個(gè)子圖季稳,rect=[左, 下, 寬, 高]擅这,是使用的絕對(duì)布局,不和以存在窗口擠占空間
axes1.plot(x,y) #在子圖上畫(huà)圖
plt.savefig('aa.jpg',dpi=400,bbox_inches='tight') #savefig保存圖片景鼠,dpi分辨率仲翎,bbox_inches子圖周邊白色空間的大小
plt.show() #打開(kāi)窗口,對(duì)于方法1創(chuàng)建在窗口一定繪制铛漓,對(duì)于方法2方法3創(chuàng)建的窗口溯香,若坐標(biāo)系全部空白,則不繪制