1.描點作圖
讓我們從最簡單的描點作圖開始。
直接上結論:
import matplotlib.pyplot as plt
plt.figure(figsize = (15, 10))
x_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_list = [27, 29, 35, 36, 24, 22, 8, 6, 11, 38]
plt.plot(x_list, y_list)
plt.xlabel('time')
plt.ylabel('price')
plt.legend()
加點兒講解,已經很熟悉或者一眼就能看懂的略過下面一段吧。
import matplotlib.pyplot as plt
# 導入plt
plt.figure(figsize = (15, 10))
# 這個是控制圖表大小的
x_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 橫軸數據,列表形式
y_list = [27, 29, 35, 36, 24, 22, 8, 6, 11, 38]
# 縱軸數據,列表形式
plt.plot(x_list, y_list)
# plt.plot(橫軸數據列表,縱軸數據列表)
plt.xlabel('time')
# 橫軸標籤
plt.ylabel('price')
# 縱軸標籤
plt.legend()
# 繪圖
效果:
2.雙摺線
我們來搞點複雜的吧。
import matplotlib.pyplot as plt
plt.figure(figsize = (15, 10))
Ax_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Ay_list = [27, 29, 35, 36, 24, 22, 8, 6, 11, 38]
plt.plot(Ax_list, Ay_list, label='A')
Bx_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
By_list = [11, 24, 31, 18, 4, 22, 18, 23, 24, 32]
plt.plot(Bx_list, By_list, label='B')
plt.xlabel('time')
plt.ylabel('price')
plt.legend()
效果如下:
3.一組list表示雙摺線
import matplotlib.pyplot as plt
plt.figure(figsize = (15, 10))
ABxy_list = [[27, 11], [29, 24], [35, 31], [36, 18], [24, 4], [22, 22], [8, 18], [6, 23], [11, 24], [38, 32]]
plt.plot([i[0] for i in ABxy_list], label='A')
plt.plot([i[1] for i in ABxy_list], label='B')
plt.xlabel('time')
plt.ylabel('price')
plt.legend()
4.指定線條顏色
好,來整點兒五顏六色、花裏胡哨的:
import matplotlib.pyplot as plt
plt.figure(figsize = (15, 10))
ABxy_list = [[27, 11], [29, 24], [35, 31], [36, 18], [24, 4], [22, 22], [8, 18], [6, 23], [11, 24], [38, 32]]
plt.plot([i[0] for i in ABxy_list], label='A', color='cyan')
plt.plot([i[1] for i in ABxy_list], label='B', color='#cc33ff')
plt.xlabel('time')
plt.ylabel('price')
plt.legend()
效果如下: