Python - Matplotlib

Python - Matplotlib

LAVI

Course

可以參考我的課程簡報:

Python Introduction

Matplotlib 繪製教學

Install

1
pip install matplotlib 

pyplot

畫折線圖

1
2
3
4
5
6
7
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196, 197, 198, 201, 205]
y = [200, 400, 450, 580, 665, 790, 1000, 1262, 2525, 3018, 3682, 4061, 310, 6520]

plt.plot(x, y)
plt.show()

setting color

設定線段顏色

1
2
3
4
5
6
7
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196, 197, 198, 201, 205]
y = [200, 400, 450, 580, 665, 790, 1000, 1262, 2525, 3018, 3682, 4061, 310, 6520]

plt.plot(x, y, color = (255/255, 100/255, 100/255))
plt.show()

axis label & title

加上圖片標題、x 軸 y 軸名稱

1
2
3
4
5
6
7
8
9
10
11
12
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196, 197, 198, 201, 205]
y = [200, 400, 450, 580, 665, 790, 1000, 1262, 2525, 3018, 3682, 4061, 310, 6520]

plt.plot(x, y, color = (255/255, 100/255, 100/255))

plt.title("This is title")
plt.ylabel("This is y label")
plt.xlabel("This is x label")

plt.show()

multiple data

同時要畫多條線的時候

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196197, 198, 201, 205]
yl = [2000, 4000, 4500, 5800, 6650, 7900, 1000, 1262, 2525, 3018, 3682, 4061, 5310, 6520]

y2 = [1000, 3000, 3500, 4500, 3650, 4900, 8000, 1032, 1525, 2010, 2682, 3061, 4310, 5520]

plt.plot(x, y1, color = (255/255, 100/255, 100/255))
plt.plot(x, y2, '--',color = (100/255, 100/255, 255/255))

plt.title("This is title")
plt.ylabel("This is y label")
plt.xlabel("This is x label")

plt.show()

grid

可以在圖上加上網格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196197, 198, 201, 205]
yl = [2000, 4000, 4500, 5800, 6650, 7900, 1000, 1262, 2525, 3018, 3682, 4061, 5310, 6520]

y2 = [1000, 3000, 3500, 4500, 3650, 4900, 8000, 1032, 1525, 2010, 2682, 3061, 4310, 5520]

plt.plot(x, y1, color = (255/255, 100/255, 100/255))
plt.plot(x, y2, '--',color = (100/255, 100/255, 255/255))

plt.title("This is title")
plt.ylabel("This is y label")
plt.xlabel("This is x label")

plt.grid(True)
plt.show()

setp

可以在線段上加點原點,方便標示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196197, 198, 201, 205]
yl = [2000, 4000, 4500, 5800, 6650, 7900, 1000, 1262, 2525, 3018, 3682, 4061, 5310, 6520]

y2 = [1000, 3000, 3500, 4500, 3650, 4900, 8000, 1032, 1525, 2010, 2682, 3061, 4310, 5520]

lines = plt.plot(x, y1, x, y2)

plt.title("This is title")
plt.ylabel("This is y label")
plt.xlabel("This is x label")

plt.setp(lines, marker = "o")

plt.grid(True)
plt.show()

legend

小標籤,可以標示線段、顏色之類的涵義

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import matplotlib.pyplot as plt

X = [100, 150, 160, 170, 175, 180, 185, 190, 195, 196197, 198, 201, 205]
yl = [2000, 4000, 4500, 5800, 6650, 7900, 1000, 1262, 2525, 3018, 3682, 4061, 5310, 6520]

y2 = [1000, 3000, 3500, 4500, 3650, 4900, 8000, 1032, 1525, 2010, 2682, 3061, 4310, 5520]

lines = plt.plot(x, y1, x, y2)

plt.title("This is title")
plt.ylabel("This is y label")
plt.xlabel("This is x label")

plt.setp(lines, marker = "o")
plt.legend(["blue", "orange"], loc = "lower right")

plt.grid(True)
plt.show()

Reference