- 學習matplotlib畫圖已經有一段時間了,各種操作都做了一遍,但是還是有點迷糊,尤其是座標軸上各個部分的名稱,在此特意將matplotlib上的經典例圖的各部分名稱全部總結在此,做個總結,以後再做操作上補充。
主要圖像要素的名稱
- Title
- 圖名(在整個圖的上面)
- label
- 標籤(在座標軸的邊上)
- 應用
- 座標軸標籤自適應
- fig.autofmt_xdate()
- 設置y軸標籤
- ax1.set_ylabel(‘Y1’)
- 座標軸標籤自適應
- tick
- 刻度
- 設置座標軸有多少格
- ax.locator_params(‘x’, nbins = 10) # 設置x軸有10格組成
- legend
- 圖例
- 應用
- plt.legend(loc = 0, ncol = 3) # 0代表最佳位置,3表示有三列
- ax.legend()
- grid
- 背景網格
- 應用
- plt.grid(True)
- ax.grid(True)
- text
- 文字註釋(可以用TeX公式)
- 應用
- plt.text()
- ax.text(2,4,r"$ \alpha_i \beta_j \pi \lambda \omega $", size = 25)
- axis
- 座標軸
- xmin, xmax, ymin, ymax = axis()
- xmin, xmax, ymin, ymax = axis([xmin, xmax, ymin, ymax])
- xmin, xmax, ymin, ymax = axis(option)
- xmin, xmax, ymin, ymax = axis(**kwargs)
- 參數可以爲下列單詞
============================================================
Value | Description |
---|---|
‘on’ | Turn on axis lines and labels. Same as True . |
‘off’ | Turn off axis lines and labels. Same as False . |
‘equal’ | Set equal scaling (i.e., make circles circular) bychanging axis limits. |
‘scaled’ | Set equal scaling (i.e., make circles circular) by changing dimensions of the plot box. |
‘tight’ | Set limits just large enough to show all data. |
‘auto’ | Automatic scaling (fill plot box with data). |
‘normal’ | Same as ‘auto’; deprecated. |
‘image’ | ‘scaled’ with axis limits equal to data limits. |
‘square’ | Square plot; similar to ‘scaled’, but initially forcing xmax-xmin = ymax-ymin |
===========================================================
-
應用
- 可以自己定製座標範圍
- plt.axis([-100,100,-10,200])
- 單獨調節某個參數
- plt.xlim([-5,5])
- 添加座標軸
- plt.twinx()
- 可以自己定製座標範圍
-
figure
- 畫板,是畫紙的載體
-
https://www.zhihu.com/question/51745620
-
Axes/Subplot
-
畫紙 軸域 ~=子圖
-
axes(軸域)可以理解一些軸的集合:axes不是數學上的座標軸,而是圖形結構中的一個對象。所有繪圖函數都是直接作用在當前axes對象上。
-
subplot子圖就是畫板上的畫紙
-
應用
- plt.subplot(221)
- fig = plt.figure()
- fig.add_axes([0.1, 0.1, 0.8, 0.8])
- fig.add_subplot(111)
-
https://www.zhihu.com/question/51745620(這裏面還有作者對於面向對象和麪向過程編程的思考和建議哦)
-
-
spines
- 軸脊線-記錄數據區域邊界的線
- gca
- get current axes
- 獲取圖像的軸,總共有四個軸top、bottom、left和right
- plt.spines[‘top’]
- …
- plt.spines[‘top’].set_color()
- plt.spines[‘right’].set_color(‘none’)(隱藏座標軸)
- …
- plt.spines[‘top’].set_position()
主要函數的參數
- color
- 顏色
- marker
- 點型
-
- linestyle
- 線型
- linewidth
- 線寬
- loc
- 位置
- ncol
- 列
- nbins
- 格數
- size
- 大小
- family
- 字體
- style
- 字體風格
- bbox
- 邊框
- alpha
- 透明度
- weight
- 粗細