1.Figure和Subplot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import numpy as np import matplotlib.pyplot as plt #創建一個Figure fig = plt.figure() #不能通過空figure繪圖,必須使用add_subplot創建一個或多個subplot #圖像為2x2,第三個參數為當前選中的第幾個 ax1 = fig.add_subplot( 2 , 2 , 1 ) ax2 = fig.add_subplot( 2 , 2 , 2 ) ax3 = fig.add_subplot( 2 , 2 , 3 ) #默認在最后一個subplot上繪制 #'k--'為線型選項,繪制黑色虛線 plt.plot(np.random.randn( 50 ).cumsum(), 'k--' ) print ( type (ax1)) #<class 'matplotlib.axes._subplots.AxesSubplot'> #直接調用它們的實例方法就可以在其他格子繪圖 _ = ax1.hist(np.random.randn( 100 ), bins = 20 , color = 'k' , alpha = 0.3 ) ax2.scatter(np.arange( 30 ), np.arange( 30 ) + 3 * np.random.randn( 30 )) plt.show() |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
fig, axes = plt.subplots( 2 , 2 , sharex = True , sharey = True ) #創建一個新的Figure,并返回一個已創建subplot對象的NumPy數組 #可以索引axes[0,1],axes[0][1] ''' plt.subplots的選項 nrows:subplot的行數 ncols:subplot的列數 sharex:所有subplot應該使用相同的x軸刻度(調節xlim將會影響所有subplot) sharey:所有subplot應該使用相同的y軸刻度(調節ylim將會影響所有subplot) subplot_kw:用于創建各subplot的關鍵字字典 **fig_kw:創建figure時其他關鍵字,如plt.subplots(2,2,figsize=(8,6)) ''' for i in range ( 2 ): for j in range ( 2 ): axes[i,j].hist(np.random.randn( 500 ),bins = 50 , color = 'k' ,alpha = 0.5 ) #調整subplot周圍間距 #plt.subplots_adjust(left=None,bottom=None,right=None,top=None,wspace=None,hspace=None) plt.subplots_adjust(wspace = 0 , hspace = 0 ) plt.show() |
2.顏色、標記和線型
1
2
3
4
5
6
7
8
9
|
#ax.plot(x,y,'g--') #ax.plot(x, y, linestyle='--', color='g') #plt.plot(np.random.randn(30).cumsum(), 'ko--') #plt.plot(np.random.randn(30).cumsum(), color='k', linestyle='dashed', marker='o') #線型圖中,非實際數據點默認是按線性方式插值的,可以通過drawstyle選項修改 data = np.random.randn( 30 ).cumsum() plt.plot(data, 'k--' , label = 'Defalt' ) plt.plot(data, 'k-' , drawstyle = 'steps-post' , label = 'steps-post' ) plt.legend(loc = 'best' ) |
3.刻度、標簽和圖例
xlim,xticks,xticklabels之類的方法。它們分別控制圖表的范圍、刻度位置、刻度標簽等。
其使用方式有以下兩種:
- 調用時不帶參數,則返回當前參數值。plt.xlim()
- 調用時帶參數,則設置參數值。plt.xlim([0,10])
這些方法對當前或最近創建的AxesSubplot起作用
對應在subplot對象上的兩個方法,如ax.get_xlim和ax.set_xlim
3.1.設置標題、軸標簽、刻度以及刻度標簽
1
2
3
4
5
6
7
8
9
|
fig = plt.figure() ax = fig.add_subplot( 1 , 1 , 1 ) ax.plot(np.random.randn( 1000 ).cumsum()) #改變X軸的刻度,最簡單的方法是使用set_xticks和set_xticklabels。 #前者告訴刻度放在數據范圍中的哪些位置,默認情況下,這些位置是刻度標簽,可以用set_xticklabels設置。 a = ax.set_xticks([ 0 , 250 , 500 , 750 , 1000 ]) b = ax.set_xticklabels([ 'one' , 'two' , 'three' , 'four' , 'five' ],rotation = 30 ,fontsize = 'small' ) ax.set_xlabel( 'Stages' ) plt.show() |
3.2.添加圖例(legend)、注解以及在Subplot上繪圖
兩種方式,最簡單的是在添加subplot的時候傳入label參數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
fig = plt.figure() ax = fig.add_subplot( 1 , 1 , 1 ) ax.plot(np.random.randn( 1000 ).cumsum(), 'k' , label = 'one' ) ax.plot(np.random.randn( 1000 ).cumsum(), 'k--' , label = 'two' ) ax.plot(np.random.randn( 1000 ).cumsum(), 'k.' , label = 'three' ) ax.legend(loc = 'best' ) #loc表示將圖例放在哪 #從圖例中去除一個或多個元素,不傳入label或label='_nolegend_'即可 #注解以及在Subplot上繪圖 #注解可以通過text,arrow和annotate等函數進行添加。 #text可以將文本繪制在圖標的指定坐標(x,y),還可以加上一些自定義格式 #ax.text(x ,y, 'Hello world!',family='monosapce',fontsize=10) plt.show() |
3.3.將圖表保存到文件
plt.savefig('filepath.svg')
plt.savefig('filepath.svg', dpi=400,bbox_inches='tight')
Figure.savefig參數
- fname:路徑,包含設置文件格式(如.pdf等)
- dpi:圖像分辨率,默認100
- facecolor、edgecolor:圖像背景色,默認為'w'(白色)
- format:顯示設置文件格式
- bbox_inches:圖像需要保存的部分。'tight',將嘗試剪除圖像周圍的空白部分
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/zhangxiaoman/p/12661134.html