# Matplotlib Chapter 1
[TOC]
###### tags: `Matplotlib tutorial`
## Matplotlib 模組介紹與安裝
### Matplotlib
matplotlib是Python語言及其數值計算庫NumPy的繪圖庫。它提供了一個物件導向的API,用於使用通用GUI工具包(如Tkinter、wxPython、Qt或GTK)將繪圖嵌入到應用程式中。

[matplotlib 官方網站](https://matplotlib.org/)
### 安裝 - pip install
```shell=
$ pip3 install matplotlib
```
在 python script 中的開頭引用matplotlib進來
```python=
import matplotlib
```
## Matplotlib Pyplot
Pyplot 是 Matplotlib 的子庫,提供了和 MATLAB 類似的繪圖 API。
Pyplot 是常用的繪圖模組,能很方便讓使用者繪製 2D 圖表。
在 python script 中的開頭引用matplotlib.pyplot進來
```python=
import matplotlib.pyplot as plt
```
## 繪圖
兩點連一線,我們可以給予兩格座標繪製出一條直線
```python=
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0, 5])
y = np.array([0, 500])
plt.plot(x, y)
plt.show()
```

- **plot()**
plot函數可以繪製點與線
```python=
# 單一條線
plot([x], y, [fmt], *, data=None, **kwargs)
# 多條線
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
```
1. x,y: 為點或是線條資料(節點),可列表成為list
2. fmt: 選擇性參數,定義基本的格式
3. 如果不指定 x 軸上的點,則x會根據y的值來設置為 0, 1, 2,...,N-1
:::info
繪製sin, cos fumction
```python=
import matplotlib.pyplot as plt
import numpy as np
# plot sin , cos funcion
x = np.arange(0,3*np.pi,0.1) # start, stop, step
y_cos = np.cos(x)
y_sin = np.sin(x)
plt.plot(x,y_cos, x,y_sin)
plt.show()
```
輸出結果

:::
## 繪圖標記
### **marker參數**
| 標記 | 描述 |
| ------------------ | -------------------------------------------- |
| "." | 點 |
| "," | 像素點 |
| "o" | 實心圓 |
| "v" | 下三角 |
| "^" | 上三角 |
| "<" | 左三角 |
| ">" | 右三角 |
| "1" | 下三叉 |
| "2" | 上三叉 |
| "3" | 左三叉 |
| "4" | 右三叉 |
| "8" | 八角形 |
| "s" | 正方形 |
| "p" | 五邊形 |
| "P" | 加號(填充) |
| "\*" | 星號 |
| "h" | 六邊形 1 |
| "H" | 六邊形 2 |
| "+" | 加號 |
| "x" | 乘號 x |
| "X" | 乘號 x (填充) |
| "D" | 菱形 |
| "d" | 瘦菱形 |
| "\| " | 豎線 |
| "\_" | 橫線 |
| 0 (TICKLEFT) | 左橫線 |
| 1 (TICKRIGHT) | 右橫線 |
| 2 (TICKUP) | 上豎線 |
| 3 (TICKDOWN) | 下豎線 |
| 4 (CARETLEFT) | 左箭頭 |
| 5 (CARETRIGHT) | 右箭頭 |
| 6 (CARETUP) | 上箭頭 |
| 7 (CARETDOWN) | 下箭頭 |
| 8 (CARETLEFTBASE) | 左箭頭 (中間點為基準) |
| 9 (CARETRIGHTBASE) | 右箭頭 (中間點為基準) |
| 10 (CARETUPBASE) | 上箭頭 (中間點為基準) |
| 11 (CARETDOWNBASE) | 下箭頭 (中間點為基準) |
| "None", " " or "" | 沒有任何標記 |
| '$...$' | 渲染指定的字符。例如 "$f$" 以字母 f 為標記。 |
:::info
Example
```python=
import matplotlib.pyplot as plt
## marker
x = [25,26,27,28,29,30,31,32,33,34,35]
y = [1569,2484,3641,4569,5492,6498,7132,8561,9132,10253,11598]
plt.plot(x,y, marker='+')
plt.show()
```
Result

:::
### **fmt 參數**
fmt(format)參數定義了**基本格式**,像是標記、線條樣式、顏色。
```python=
fmt = '[marker][line][color]'
```
- 線類型
| 線類型標記 | 說明 |
| ---------- | ------ |
| '-' | 實線 |
| ':' | 虛線 |
| '--' | 破折線 |
| '-.' | 點划線 |
- 顏色類型
| 顏色標記 | 說明 |
| -------- | ---- |
| 'r' | 紅色 |
| 'g' | 綠色 |
| 'b' | 藍色 |
| 'c' | 青色 |
| 'm' | 品紅 |
| 'y' | 黃色 |
| 'k' | 黑色 |
| 'w' | 白色 |
:::info
Example
```python=
import matplotlib.pyplot as plt
## fmt
x = [25,26,27,28,29,30,31,32,33,34,35]
y = [1569,2484,3641,4569,5492,6498,7132,8561,9132,10253,11598]
plt.plot(x,y, 'D-.r')
plt.show()
```
Result

:::
### **標記顏色與大小**
- **markersize (ms)**: 定義標記的大小
- **markerfacecolor (mfc)**: 定義標記內部的顏色
- **markeredgecolor (mec)**: 定義標記邊框的顏色
:::info
Example
```python=
import matplotlib.pyplot as plt
x = [25,26,27,28,29,30,31,32,33,34,35]
y = [1569,2484,3641,4569,5492,6498,7132,8561,9132,10253,11598]
plt.plot(x,y, marker = 'o', ms = 10, mfc = 'w', mec='r')
plt.show()
```
Result

:::
## 繪圖線 (Plot Line)
### 線的類型 (linestyle, `ls`)
- 標記同**fmt**的類型代號
### 線的顏色 (color, `c`)
- 標記同**fmt**的顏色代號
- **自訂義顏色類型**: #RRGGBB 格式, 或是特殊顏色的名稱, 可以參考[HTML 顏色值](https://www.runoob.com/html/html-colorvalues.html)
### 線的寬度 (linewidth, `lw`)
- 定義線的寬度,可以是浮點術或整數
:::info
Example
```python=
import matplotlib.pyplot as plt
## Line style config
x = [25,26,27,28,29,30,31,32,33,34,35]
y = [1569,2484,3641,4569,5492,6498,7132,8561,9132,10253,11598]
plt.plot(x,y, linestyle=':', color='g', linewidth='4.56')
plt.show()
```
Result

:::
### 繪製多條線
```python=
import matplotlib.pyplot as plt
## plot multi-line
x = [25,26,27,28,29,30,31,32,33,34,35]
y_1 = [1569,2484,3641,4569,5492,6498,7132,8561,9132,10253,11598]
y_2 = [11456,10892,9455,8913,7428,6287,7428,8913,9455,10892,11456]
plt.plot(x,y_1,x,y_2)
plt.show()
```
結果

## 參考資料
[matplotlib - RUNOOB.COM](https://www.runoob.com/matplotlib/matplotlib-tutorial.html)
[莫煩 PYTHON - Matplotlib](https://mofanpy.com/tutorials/data-manipulation/plt/)