Python——繪圖


1、保存圖片。

fig.savefig

一、創建畫布

1、創建畫布和坐標軸

在Matplotlib中,plt.figure類可以看做一個能夠容納各種坐標軸、圖形、文字和標簽的容器。plt.Axes類是一個帶有刻度和標簽的矩形,最終會包含所有可視化的圖形元素。

此處,fig代表一個圖例,ax表示一個坐標軸實例或一組坐標軸實例。

%matplotlib inline

import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes()

x = np.linspace(0,10,1000)
ax.plot(x, np.sin(x))

 

2、面向對象風格接口

#面向對象的風格
#創建圖形網絡
#ax是一個包含兩個Axes對象的數組,即有兩個坐標軸
fig,ax = plt.subplots(2)

#在每個Axes對象上調用的plot()方法,分別繪制sin()和cos()
ax[0].plot(x, np.sin(x)) 
ax[1].plot(x, np.cos(x))

 

3、Matlab風格接口

plt.figure()#創建圖形

#Matlib風格接口
#創建兩個子圖中的第一個,設置坐標軸,等於fig,ax=plt.subplot()
plt.subplot(2, 1, 1)  
plt.plot(x, np.sin(x))

#創建兩個子圖中的第一個,設置坐標軸
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x))

 

 
        

 

 

二、坐標軸和線條調整

1、調整線條顏色和樣式

 

線條樣式:

'-'       solid line style
'--'      dashed line style
'-.'      dash-dot line style
':'       dotted line style

 

顏色:

'.'       point marker
','       pixel marker
'o'       circle marker
'v'       triangle_down marker
'^'       triangle_up marker
'<'       triangle_left marker
'>'       triangle_right marker
'1'       tri_down marker
'2'       tri_up marker
'3'       tri_left marker
'4'       tri_right marker
's'       square marker
'p'       pentagon marker
'*'       star marker
'h'       hexagon1 marker
'H'       hexagon2 marker
'+'       plus marker
'x'       x marker
'D'       diamond marker
'd'       thin_diamond marker
'|'       vline marker
'_'       hline marker

 

plt.axhline(y=1, ls='.', c-'yellow')#增加水平線

plt.axvline(x=1,ls='-',c='red') #增加垂直線

 

2、調整坐標軸

(1)調整坐標軸上下限:

plt.xlim()  #等價於ax.set_xlim()

plt.ylim()  #等價於ax.set_ylim()

plt.axis([xmin, xmax, ymin, ymax])

(2)設置圖形標簽

plt.title()  #設置圖形標題,等價於ax.set_title()

plt.xlabel(), plt.ylabel()  #設置X,Y軸標題,等價於ax.set_xlabel(), ax.set_ylabel()

(3)配置圖例

plt.legend()  #創建圖例

ax.legend(frameon=False, loc='epper left')

#選擇圖例顯示的元素
#方式一
plt.legend(lines[:2], ['first','second'])

#方式二
plt.plot(x, y[:,0], label='frist')
plt.plot(x, y[:,1], label='second')
plt.plot(x,y[:,2:])
plt.legend(gramealpha=1,frameon=True)#默認情況下會忽略那些不帶標簽的元素

 

三、多子圖

1、圖中圖

plt.axes([bottom, left, width, height] #[底坐標,坐坐標,寬度,高度]

#xample1
ax1 = plt.axes()
ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])

#example2
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4],
                                xticklabels=[], ylim=(-1.2, 1.2))
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4],
                                ylim=(-1.2, 1.2))
x = np.linspace(0, 10)
ax1.plot(np.sin(x))
ax2.plot(np.cos(x))

2、簡易網格子圖

plt.subplot(行數,列數,索引值)

for i in range(1,7):
  plt.subplot(2,3,i)
  plt.text(0.5, 0.5, str((2,3,i)),
      fontsizt=18, ha='center')

fig = plt.figure() #plt.subplot_adjust可以調整子圖之間的間隔 fig.subplots_adjust(hspace=0.4, wspace=0.4) for i in range(1,7): ax=fig.add_subplot(2, 3, i)
   ax.text(0.5, 0.5, str((2,3,i)),
      fontstze=18, ha='center')

 等價於

plt.subplots(2, 3, sharex='col', sharey='row')

#比較subplot & subplots

#subplots_addjust

 

四、文字與注釋

ax.text():文字注釋

ax.transData:用x軸與y軸標簽作為數據坐標
ax.transAxes:以坐標軸左下角為原點,按照坐標軸尺寸的比例呈現坐標.
fig.transFigure:以圖形左下角為原點,按照圖形尺寸的比例呈現坐標。

fig, ax = plt.subplots(facecolor='lightgray')
ax.axis([0, 10, 0, 10])

ax.text(1, 5, ".data:(1,5)", transform=ax.transData)
ax.text(0.5, 0.2, ".Axes:(0.5, 0.2)", transform=ax.transAxes)
ax.text(0.5, 0.2, ".Figure:(0.5, 0.2)", transform=fig.transFigure)

plt.annotate():創建箭頭

 

參考:《Python數據科學手冊》

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM