# 資研 5/24 上課講義
[**提問表單**](https://forms.gle/z4dPC1JPV6uA2q9w7)
---
## Matplotlib
[**官方資料**](https://matplotlib.org/stable/index.html)
### 上堂課
:::info
**練習**
請大家畫一個 $f1: y = (x-\frac{1}{5})^2 + 2$ 和一個 $f2: y = -x^3 + 2$ 的圖形,條件如下:
1. 圖的大小是 $8 * 8$
2. 標題是 **二次函數** 字體大小為 15
4. x是在-2~2之間平均取20個點(上次有教)
5. 要畫線+描點
6. f1的顏色為 #005AB5 ;f2的顏色為 #3D7878
7. 要有圖例標示
8. 其餘的自訂
```python=
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '字型路徑')
# 定義出指定函式
def f1(x):
return (x-0.2)**2+2
def f2(x):
return -x**3+2
# 設定座標點
x_point = np.linspace(-2, 2, 20)
y_point1 = f1(x_point)
y_point2 = f2(x_point)
# 定義圖片大小、標題
plt.figure(figsize = (8, 8))
plt.title("二次函數", fontproperties = twfont1, fontsize = 15)
plt.xlabel("x軸", fontproperties = twfont1, fontsize = 8)
plt.ylabel("y軸", fontproperties = twfont1, fontsize = 8)
# 畫出來
plt.plot(x_point, y_point1, c = "#005AB5", linewidth = 2, marker='o', ms = 5, label = "f1")
plt.plot(x_point, y_point2, linestyle = "dotted", c = "#3D7878", linewidth = 2, marker='s', ms = 5, label = "f2")
plt.legend(prop = twfont1)
```
範例:
>不一定會一模一樣

:::
### 散佈圖 Scatter
```python=
import numpy as np
import random as rd
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '字型檔案')
plt.figure(figsize = (5, 5))
plt.title("散佈圖", fontproperties = twfont1, fontsize = 20)
x_point = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y_point = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
# 散佈圖用scatter
plt.scatter(x_point, y_point, c = "#7373B9")
# 沒有圖例,用plt.show()
plt.show()
```

:::warning
**顏色可以用漸層的**
[cmps](https://matplotlib.org/stable/users/explain/colors/colormaps.html)
```python=
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '字型檔案')
plt.figure(figsize = (5, 5))
plt.title("散佈圖", fontproperties = twfont1, fontsize = 20)
x_point = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y_point = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.linspace(0, 100, 13)
plt.scatter(x_point, y_point, c = colors, cmap = "Blues")
# 顯示colorbar
plt.colorbar()
plt.show()
```

:::
### 長條圖 Bars
```python=
import numpy as np
import random as rd
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '/content/drive/MyDrive/資研/資料/msjh.ttc')
plt.figure(figsize = (10, 5))
plt.title("長條圖", fontproperties = twfont1, fontsize = 20)
plt.xlabel("座號", fontproperties = twfont1, fontsize = 10)
plt.ylabel("作業完成度", fontproperties = twfont1, fontsize = 10)
# 取得 array = 1~38
x_point = np.arange(1, 39)
# 取得 array = 1~100
Y = list(np.arange(1, 101))
# 隨機取 38個1~100之間 的數字做為 1~38號的完成狀況
y_point = rd.sample(Y, 38)
# 長條圖用bar
plt.bar(x_point, y_point)
plt.show()
```

:::info
**練習**
請利用以下資料,先將作業完成度轉換成完成百分比,並畫出長條圖
```
座號 完成的作業數量(共17項)
1 16
2 13
3 4
4 12
5 11
6 15
7 3
8 12
9 12
10 7
11 8
12 6
13 7
14 15
15 1
16 17
17 10
18 17
19 14
20 13
```
```python=
import numpy as np
import random as rd
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '字型路徑')
plt.figure(figsize = (10, 5))
plt.title("長條圖", fontproperties = twfont1, fontsize = 20)
plt.xlabel("座號", fontproperties = twfont1, fontsize = 10)
plt.ylabel("作業完成度%", fontproperties = twfont1, fontsize = 10)
x_point = np.arange(1, 21)
y = np.array([16, 13, 4, 12, 11, 15, 3, 12, 12, 7, 8, 6, 7, 15, 1, 17, 10, 17, 14, 13])
y_point = y/17*100
# 長條圖用bar
plt.bar(x_point, y_point)
plt.show()
```
範例

:::
### 直方圖 Histograms
```python=
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '字型檔案')
plt.figure(figsize = (10, 5))
plt.title("長方圖", fontproperties = twfont1, fontsize = 20)
D = list(np.arange(0, 100))
data = rd.sample(D, 20)
# 直方圖用plt.hist(),bins = "auto"會自動決定組距
plt.hist(data, bins = 'auto')
plt.show()
```

### 圓餅圖 Pie Charts
```python=
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_font
twfont1 = plt_font.FontProperties(fname = '字型檔案')
plt.figure(figsize = (10, 5))
plt.title("圓餅圖", fontproperties = twfont1, fontsize = 20)
data = np.array([35, 25, 25, 15])
# 設定各項數據名稱
L = ["freshman", "sophomore", "junior", "senior"]
# 設定圓餅突出的距離
E = [0, 0.2, 0, 0]
# 設定顏色
C = ["#C4E1FF", "#66B3FF", "#2894FF", "#004B97"]
# 圓餅圖用plt.pie(),labels圖例,explode突出,colors顏色,shadow陰影
plt.pie(data, labels = L, explode = E, colors = C, shadow = True)
# title標題,loc圖例位置
plt.legend(prop = twfont1, title = "Students", loc = "upper left")
```
