vault backup: 2024-02-17 23:11:19
This commit is contained in:
72
05. 資料收集/Programming/Python/matplotlib.md
Normal file
72
05. 資料收集/Programming/Python/matplotlib.md
Normal file
@@ -0,0 +1,72 @@
|
||||
## 基本折線圖
|
||||
給2個list,一個 x,一個 y
|
||||
```python
|
||||
plt.clf() # 把圖清掉,變空白
|
||||
plt.plot(xList, yList)
|
||||
```
|
||||
|
||||
## XY軸標籤
|
||||
```python
|
||||
plt.xlabel(
|
||||
'Focus setting', # 標籤
|
||||
fontsize=15, # 字型大小
|
||||
labelpad=10, # 標籤留白
|
||||
color='red', # 文字顏色
|
||||
rotation=90, # 文字旋轉角度
|
||||
fontweight='bold', # 粗體
|
||||
)
|
||||
```
|
||||
|
||||
## 不要顯示軸的刻線
|
||||
```python
|
||||
plt.gca().axes.get_xaxis().set_visible(False)
|
||||
```
|
||||
|
||||
## 畫2張圖
|
||||
```python
|
||||
figure, axis = plt.subplots(2, 2)
|
||||
```
|
||||
用 `plt.subplots()` 來指定要畫幾張圖,第一個參數是要有幾個 row,第二個參數是要有幾個 column。
|
||||
`axis` 會是一個 array,可以用類似座標的方式來控制你要的圖,例如:
|
||||
```python
|
||||
axis[0, 0].set_title("Sine Function")
|
||||
axis[0, 1].set_title("Cosine Function")
|
||||
```
|
||||
|
||||
`figure` 則是指外圍的大圖。
|
||||
|
||||
## 畫2條線
|
||||
```python
|
||||
plt.plot(x, y1, label='sine curve',color='b')
|
||||
plt.plot(x, y2, label='cosine curve',color='r')
|
||||
```
|
||||
|
||||
## 畫大圖
|
||||
```python
|
||||
figure(figsize=(12, 9), dpi=120)
|
||||
```
|
||||
`12`跟`9`指的是英吋,`dpi`是每英吋幾個點,所以就是`12*120`跟`9*120`,也就是`1440x1080`。
|
||||
|
||||
## 存檔
|
||||
```python
|
||||
plt.savefig(f'plot_{folder}.png')
|
||||
```
|
||||
|
||||
## 註記(annotation)
|
||||
```python
|
||||
ax = plt.gca()
|
||||
ax.annotate(
|
||||
'local max', # 註記文字
|
||||
xy=(xmax, ymax), # 點的座標
|
||||
xytext=(xmax, ymax + 5), # 文字的座標
|
||||
arrowprops=dict( # 箭頭的屬性
|
||||
facecolor='black', # 顏色:黑色
|
||||
shrink=0.05), #
|
||||
)
|
||||
```
|
||||
官方說明:[matplotlib.axes.Axes.annotate — Matplotlib 3.7.1](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.annotate.html)
|
||||
|
||||
## 在X軸上畫一個範圍
|
||||
```python
|
||||
plt.gca().axvspan(startXPos, endXPos, alpha=0.2, color='red')
|
||||
```
|
||||
Reference in New Issue
Block a user