Matplotlib
(Matlab Plot Library)
講師:林志偉
⽤Matplotlib 畫圖
# import library
from matplotlib import pyplot as plt
import numpy as np
# prepare data
x = [Link](0, 10, 200)
y = [Link](5*x) / (1 + x**2)
# capable to display 繁體字
[Link]['[Link]-serif']=['Heiti TC']
# 調整圖型參數
[Link]('振幅越來越⼩的 sin')
# 畫圖看結果
[Link](x,y)
[Link]()
Matplotlib
l Matplotlib 是⼀個資料視覺化的Python第三⽅模組,搭配 Numpy 的數值運算,Matplotlib 可以繪製的圖形
包含了⼆維與三維的函式圖、極坐標繪圖、等⾼線圖等。Matplotlib提供了相當豐富的選項,⽅便對圖形進
⾏調整
l 繪圖三步驟
ü import matplotlib: from matplotlib import pyplot as plt
ü 給數據畫圖: [Link](x 座標List, y 座標List [, 參數1, 參數2,..])
ü show 圖: [Link]()
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [2, 4, 9]
[Link](x, y)
[Link]()
若要畫的點有 (x1, y1), (x2, y2), (x3, y3), 則
• x = [x1, x2, x3]
• y = [y1, y2, y3]
折線圖 [Link]() (⽤ List 餵資料)
# 給 x list and y list # 給線的形狀跟顏⾊ # 畫兩條線⼀起 #分開畫兩條線
from matplotlib import pyplot as plt
from matplotlib import pyplot as plt from matplotlib import pyplot as plt [Link]([1,2,3], [2,4,6], from matplotlib import pyplot as plt
X = [1, 2, 3, 4, 5] X = [1, 2, 3, 4, 5] color='red', [Link](1)
Y = [2, 4, 6, 8, 10] Y = [2, 4, 6, 8, 10] lw='0.5', ls='-', [Link]([1,2,3], [1,2,3])
[Link](X, Y) [Link](X, Y, marker='*') [Link](2)
[Link]() color='red', [Link]([1,2,3], [1,4,9],
lw='0.5', ls='-', [Link]([1,2,3], [4,8,12], color='black',
marker='*') color='black', lw='1.0', ls='-.',
[Link]() lw='1.0', ls='-.', marker='^')
marker='^') [Link]()
[Link]()
#Lab
繪製折線圖
l 新⽵市每個⽉平均最⾼最低的溫度列表如下(從1⽉開始):
high = [19, 20, 22, 26, 29, 32, 34, 33, 31, 27, 24, 21]
1ow = [13, 14, 15, 19, 22, 25, 26, 26, 24, 22, 18, 15]
請把這平均最⾼溫、最低溫畫在⼀張圖上
l 畫出⼀個“M”的形狀
折線圖 [Link]() (⽤ Numpy 餵資料)
⽤ [Link] 給 x-軸值 ⽤ [Link] 給 x-軸值
import numpy as np import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt import numpy as np
from matplotlib import pyplot as plt
x = [Link](0, 9, 2) x = [Link](0, 9, 0.1) x = [Link](1, 7, 5)
y = x**2 y = x**2 # x is array[1.0, 2.5, 4.0, 5.5, 7.0]
y = x**2
[Link](x, y, marker='*') [Link](x, y) [Link](x, y, marker='*')
[Link]('Y = X^2') [Link]('Y = X^2') [Link]('Y = X^2')
[Link]() [Link]() [Link]()
折線圖參數選項
參數 說明 Example
lw (line width) 設定線條寬度,預設為 1.0 linewidth=3.0
color 設定線條顏⾊ color="red"
ls (line style) 設定線條樣式: 實線「–」 虛線「—」 虛點線「-. 」 點線 「:」 ls="-."
marker 設定標記樣式: 點「. 」 圓「o」 星「*」 矩形「s 」 三⾓型「^」 marker="*"
ms (marker size) 標記⼤⼩ ms=16
label 設定圖例名稱 (需搭配 legend 函式才有效果) label="Test"
#Lab
繪製折線圖(多線)
運⽤ matplotlib 套件繪製2段線條 #28
Ø 第1條線為男性的⾸次婚姻平均年齡的統計
• X座標串列為 year: [2006, 2011, 2014, 2015, 2016] ☑
• Y座標串列為⾸次婚姻平均年齡 age: [30.7, 31.8, 32.1, 32.2, ☑
32.4]
• 線條以實線與藍⾊來繪製
Ø 第2條線為女性的⾸次婚姻平均年齡統計
• X座標串列為 year: [2006, 2011, 2014, 2015, 2016]
• Y座標串列為⾸次婚姻平均年齡age: [27.8, 29.4, 29.9, 30.0,
30.0]
• 線條以虛線與紅⾊來繪製。
☑
Ø Add x-label, y-label, title, legend
補充指令
Ø 給線⼀個名字: [Link](x_list, y_list, color=‘red’, label=‘Female’)
[Link]()
Ø 給x軸或y軸畫圖的限制區域: [Link](2006, 2016)
Ø 給圖形標題: [Link](‘Age of first marriage’)
Ø 給x軸或y軸label: [Link](‘year’)
☑
正確顯⽰中文字
- 查看Matplotlib可⽤字型
l 在 Python shell 中鍵入下列指令
>>>import matplotlib.font_manager
>>>print(list(sorted([[Link] for f in matplotlib.font_manager.[Link]])))
l 看有沒有可⽤的中文字型,如 ‘Heiti TC’ (in Mac), 或是 ‘Microsoft JhengHei’ (in Windows)
l 若有的話, 加下⾯任意⼀⾏指令在 .py 內 (rcParams 代表 run time parameters)
Ø [Link]('font', family='Microsoft JhengHei’), or
Ø [Link]['[Link]-serif']=['Microsoft JhengHei’]
l 若沒有的話, 就必須從網路下載字形檔: : [Link]
l Install new font in windows: [Link]
l Install new font in Mac: see ‘A13 安裝新字體[Link]’
⾃學與陽明交⼤課程的學習效率比較圖
from matplotlib import pyplot as plt
import numpy as np
[Link]['[Link]-serif'] = ['Heiti TC']
days = [Link](0, 21)
self_study, nycu_python = days, days**2
[Link](days, self_study)
[Link](days, nycu_python)
[Link]([0, 5, 10, 15, 20])
[Link]('學習時間(天)')
[Link]('Python學習進度')
[Link]('nycu python 課程 vs ⾃學')
[Link]()
Subplot 繪製多張圖型
import numpy as np
from matplotlib import pyplot as
plt
x = [Link](0, 10, 5)
y1 = 2 * x
import numpy as np
y2 = 20 - 2 * x from matplotlib import pyplot as plt
x = [Link](-0, 10, 5)
[Link](2, 1, 1) y1 = 2 * x
[Link](x, y1) y2 = 20 - 2 * x
[Link]('Fig-1') # [Link](2, 1, 1)
[Link](x, y1)
[Link](2, 1, 2) # [Link]('Fig-1')
[Link](x, y2, ls='--')
# [Link](2, 1, 2)
[Link]('Fig-2') [Link](x, y2, ls='--')
[Link]() [Link]('Fig-1 & Fig-2')
[Link]()
#Lab
Subplot 多公式的折線圖
⽤ matplotlib 套件繪製公式, 共有三個⼦圖表,⼦圖表的配置為⼀列三⾏, 如下圖 (#29)
l Y=X
l Y = X^2
l Y = X^3
散佈圖(Scatter Plot)
from matplotlib import pyplot as plt
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
[Link](x, y)
[Link]()
⻑條圖 [Link]()
from matplotlib import pyplot as plt
fruits = {
'apple': 10,
'orange' : 15,
'banana' : 9,
'pears': 12,
}
fruits_type = [Link]()
fruits_number = [Link]()
[Link](fruits_type, fruits_number)
[Link]()
直⽅圖 [Link]()
#直⽅圖: 顯⽰資料的分佈
from matplotlib import pyplot as plt
from numpy import random
x = [Link](1_000)
[Link](x, 10)
[Link]()
圓餅圖 [Link]()
在⼀場選舉中, 有3位候選⼈, 以pie chart
呈現得票率
Ø Name = [‘Jason’, ‘Mary’, ‘Jeffery’]
Ø Vote = [35%, 38%, 27%]
Ø Color = [‘gray’, ‘blue’, ‘green’]
from matplotlib import pyplot as plt
name = ['Jason', 'Mary', 'Jeffery']
vote_ratio = [0.35, 0.38, 0.27]
color = ['gray', 'blue', 'green']
[Link](vote_ratio, labels=name, colors=color, autopct='%2.0f%%')
[Link]()
For Fun 畫個 xkcd 風格
from matplotlib import pyplot as plt
import matplotlib
import numpy as np
theta = [Link](0, 10*[Link], 1000)
y = [Link](theta)
[Link]()
[Link](theta, y)
[Link]()
For Fun 畫個圓吧
from matplotlib import pyplot as plt
import numpy as np
theta = [Link](0, 2*[Link], 10)
# can extend tp 1000
r=2
x = r * [Link](theta)
y = r * [Link](theta)
[Link]('equal')
[Link](x, y, color='red')
[Link]()
Backup
#Homework
海盜會導致全球暖化嗎?
l 下⽅為⼀個 csv 檔案及其內容,你可以另存新檔或⾃⼰產⽣,將檔名取為 [Link]
l 檔案裡⾯是海盜數量(Pirates)和全球溫度(Temperature)的資料
l 寫⼀個程式,讀取 [Link] 檔案並繪製圖形,x軸是海盜數量,y軸是溫度
l 把兩者的關係視覺化。在圖表裡加入標題並標記兩個軸的涵義,然後把產⽣的圖表儲存成 PNG 圖檔
l 結果應如最左圖
Secure Coding Warning MacOS Sonoma 14.3.1
[Link] [Link]
#Lab
老化指數圖
請⽤折線圖的⽅式,畫出兩條線,紅⾊線代表未滿15歲
的⼈⼝比例,藍⾊線代表65歲以上⼈⼝比例
第一條線 第二條線
#Lab
⻑條圖
運⽤ matplotlib 套件繪製2個⻑條圖
Ø 第1組為男性的⾸次婚姻平均年齡的統計
• X座標串列為 year: [2006, 2011, 2014, 2015, 2016] width = 0.35 # The width of the bars
• Y座標串列為⾸次婚姻平均年齡 age: [30.7, 31.8, 32.1, 32.2,
32.4] [Link](x - width/2, group1, width, label='Group 1')
• ⽤藍⾊來繪製 [Link](x + width/2, group2, width, label='Group 2’)
Ø 第2組為女性的⾸次婚姻平均年齡統計 [Link]('Groups')
• X座標串列為 year: [2006, 2011, 2014, 2015, 2016] [Link]('Values')
• Y座標串列為⾸次婚姻平均年齡age: [27.8, 29.4, 29.9, 30.0, [Link]('Comparison of Two Groups')
30.0] [Link]()
• ⽤紅⾊來繪製。
Ø Add x-label, y-label, title, legend
#Lab
冰雹猜想
冰雹猜想,指對於每個正整數,如果是奇數,則乘三再加⼀,如果是偶數,則除以⼆,如此循環地
往復操演,最終都能夠得到⼀
請寫程式畫圖,實作冰雹猜想