檢查是否已安裝Matplotlib
import matplotlib
matplotlib.__version__
輸出結果
'3.2.1'
import matplotlib.pyplot as plt
#plt.plot([1, 2, 3, 4])
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20])
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,20)
#x = np.arange(-1, 1, 0.15)
#x = np.linspace(-1, 1, 50)
print("x=>{}".format(x))
y = x
plt.title("Matplotlib Demo")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.plot(x, y)
plt.show()
y1 = x
y2 = x ** 2
y3 = x ** 3
plt.plot(x, y1, 'r--', x, y2, 'bs', x, y3, 'g^')
plt.show()
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.figure(figsize=(9, 3))
plt.subplot(221)
plt.bar(names, values)
plt.subplot(222)
plt.scatter(names, values)
plt.subplot(223)
plt.plot(names, values)
plt.subplot(224)
plt.plot(names, values, 'r^')
plt.suptitle('Categorical Plotting')
plt.show()
符號樣式
'.' .標記
',' ,標記
'o' 圓圈標記
'v' v標記
'^' ^標記
'<' <標記
'>' >標記
'1' 三角向下標記
'2' 三角向上標記
'3' 三角向左標記
'4' 三角向右標記
's' 方形標記
'p' 五邊形標記
'*' 星形標記
'h' 瘦六角形標記
'H' 六角形標記
'+' 加號標記
'x' X標記
'D' 鑽石標記
'd' 瘦鑽石標記
'|' |標記
'_' _標記
線條樣式
'-' 實線
'--' 虛線
'-.' 點線
':' 虛線
顏色樣式
'b' 藍色
'g' 綠色
'r' 紅色
'c' 青色
'm' 紫色
'y' 黃色
'k' 黑色
'w' 白色