# 資研 5/3 社課講義
[**提問表單**](https://forms.gle/z4dPC1JPV6uA2q9w7)
[**資料**](https://drive.google.com/drive/folders/1wdxu7tnSWGRetxjIXBvRUX06dm3LztEF?usp=sharing)
---
## 上堂課
:::info
**練習**
請試著用`random`寫出一個終極密碼(1~100)的程式
```python=
import random
seed = random.randint(1, 100)
game = True
ceil = 100
floor = 1
c = 0
while game:
ans = int(input(f"請在{floor}~{ceil}之間猜一個數字"))
if ans > seed:
print("太大囉\n")
ceil = ans
c += 1
continue
elif ans < seed:
print("太小囉\n")
floor = ans
c += 1
continue
else: # ans = seed
c += 1
break
print(f"恭喜遊戲結束,您猜對的正確答案為{seed},一共猜了{c}次")
```
:::
## Matplotlib
[**官方資料**](https://matplotlib.org/stable/index.html)
> `Matplotlib` 為套件,這次我們會教的是裡面的 `pyplot` 這個模組
```python=
import numpy as np
import matplotlib.pyplot as plt
```
:::warning
**補充**
一般在 `Matplotlib.pyplot` 中,所顯示的字型皆為英文,如果要顯示中文需要加上一些步驟
>大部分時候用英文比較保險
1. 取得字型
[**Mac的下載方式**](https://support.apple.com/zh-tw/guide/font-book/fntbk1000/mac)
檔案總管>本機>Windows>Fonts
然後右鍵複製,貼到其他地方(像是桌面),上傳到自己的雲端硬碟
>往下滑就有中文字型
>找不到的話,資料夾裡有我先下載好的
2. 掛接雲端硬碟
方法一:

點左手邊,有一個檔案的符號

點下去之後,按上方列表的第三項 — 掛接雲端硬碟 mount drive

跳出頁面,按藍色的—同意

多出現一個 Drive 資料夾就是掛接成功

>*如果沒有出現可以點第二個的重整 refresh*
>
方法二:
直接執行下方程式
```python=
from google.colab import drive
drive.mount('/content/drive')
```
執行成功即為掛接成功

3. 取得路徑

一樣點左手邊,有一個檔案的符號

點開 Drive > MyDrive 資料夾

找到自己的字型檔案,右鍵複製檔案路徑 coby path

檔案路徑就會自動處存到你的剪貼簿
4. 載入字型
```python=
# 繪圖處理套件
import matplotlib.font_manager as plt_font
# 設定中文字體物件和字型檔案路徑
twfont1 = plt_font.FontProperties(fname = 'file path')
```
<font color="#CE0000">**file path的地方要記得填入自己的檔案路徑**</font>
:::
### 關係圖 Plotting
#### 設定x、y軸的點
```python=
x_point = np.array([1, 5])
y_point = np.array([4, 10])
```
#### 設定繪圖
```python=
plt.plot(x_point, y_point)
plt.show()
```
:::spoiler 全部程式碼
```python=
import numpy as np
import matplotlib.pyplot as plt
x_point = np.array([1, 5])
y_point = np.array([4, 10])
plt.plot(x_point, y_point)
plt.show()
```
:::

#### 加上一些設定、標題、標籤
```python=
# 設定繪圖區大小
plt.figure(figsize = (12, 6))
# 繪圖區的標題
plt.title("標題說明文字", fontproperties = twfont1, fontsize = 20)
# 設定橫軸和縱軸的標題
plt.xlabel("x軸標題文字", fontproperties = twfont1, fontsize = 20)
plt.ylabel("y軸標題文字", fontproperties = twfont1, fontsize = 20)
# 函數標籤
plt.plot(x_point, y_point, label = "標籤")
# 顯示標籤
plt.legend(prop = twfont1)
```
>fontproperties 是用來設定字型, twfont1 是前面所設定的字型名稱(可改)
>fontsize 是用來設定字體的大小
>`plt.legend()` 可以取代 `plt.show()`
:::spoiler 全部程式碼
```python=
# 加入說明
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize = (5, 5))
plt.title("標題說明文字", fontproperties = twfont1, fontsize = 20)
plt.xlabel("x軸標題文字", fontproperties = twfont1, fontsize = 20)
plt.ylabel("y軸標題文字", fontproperties = twfont1, fontsize = 20)
x_point = np.array([1, 5])
y_point = np.array([4, 10])
plt.plot(x_point, y_point, label = "標籤")
plt.legend(prop = twfont1)
```
:::

#### 描點、無線
```python=
# 無線
plt.plot(x_point, y_point, 'o', label = "標籤")
```
:::spoiler 全部程式碼
```python=
# 無線
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize = (5, 5))
plt.title("標題說明文字", fontproperties = twfont1, fontsize = 20)
plt.xlabel("x軸標題文字", fontproperties = twfont1, fontsize = 20)
plt.ylabel("y軸標題文字", fontproperties = twfont1, fontsize = 20)
x_point = np.array([1, 5])
y_point = np.array([4, 10])
plt.plot(x_point, y_point, 'o', label = "標籤")
plt.legend(prop = twfont1)
```
:::

```python=
# 描點
plt.plot(x_point, y_point, marker='o', label = "標籤")
```
:::spoiler 全部程式碼
```python=
# 描點
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize = (5, 5))
plt.title("標題說明文字", fontproperties = twfont1, fontsize = 20)
plt.xlabel("x軸標題文字", fontproperties = twfont1, fontsize = 20)
plt.ylabel("y軸標題文字", fontproperties = twfont1, fontsize = 20)
x_point = np.array([1, 5])
y_point = np.array([4, 10])
plt.plot(x_point, y_point, marker='o', label = "標籤")
plt.legend(prop = twfont1)
```
:::

#### 改變顏色、線條、符號
順序是顏色、線條、符號,不用間隔直接加在資料後面
```python=
plt.plot(x_point, y_point, "r--+", marker='o', label = "標籤")
```
:::spoiler 全部程式碼
```python=
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize = (5, 5))
plt.title("標題說明文字", fontproperties = twfont1, fontsize = 20)
plt.xlabel("x軸標題文字", fontproperties = twfont1, fontsize = 20)
plt.ylabel("y軸標題文字", fontproperties = twfont1, fontsize = 20)
x_point = np.array([1, 5])
y_point = np.array([4, 10])
plt.plot(x_point, y_point, "r--+", marker='o', label = "標籤")
plt.legend(prop = twfont1)
```
:::

[**其他顏色、線條、符號**](https://www.w3schools.com/python/matplotlib_markers.asp)
[**更多跟線有關的參數**](https://www.w3schools.com/python/matplotlib_line.asp)
:::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)
```
範例:
>不一定會一模一樣

:::