matplotlib散點圖

散點圖:

繪製方法和折線圖一樣,不同之處在於繪圖方法使用的是plt.scatter()
在同一張圖中繪製多個數據的散點圖時,只需要讓每個數據對應不同的下標就行了。

實例如下:

import random
from matplotlib import pyplot as plt
import matplotlib

font = {‘family’: ‘STKAITI’,
‘weight’: ‘bold’,
‘size’: 12}
matplotlib.rc(“font”, **font)

#得到x、y軸座標
y_3 = [11,17,16,11,12,11,12,6,6,7,8,9,12,15,14,17,18,21,16,17,20,14,15,15,15,19,21,22,22,22,23]
y_10 = [26,26,28,19,21,17,16,19,18,20,20,19,22,23,17,20,21,20,22,15,11,15,5,13,17,10,11,13,12,13,6]

print(len(y_3), len(y_10))

x_3 = list(range(1, 32))
x_10 = list(range(41, 72))

##繪製刻度對應的文字
x_id = x_3[::3]+x_10[::3]
x_label_3 = [“3月{}日”.format(i) for i in x_3]
x_label_10 = [“10月{}日”.format(i) for i in x_3]
x_label = x_label_3[::3] + x_label_10[::3]
plt.xticks(x_id, x_label, rotation=45)

y_label = list(range(min(*y_3, *y_10), max(*y_3, *y_10)+1))
plt.yticks(y_label)

#圖像描述信息
plt.xlabel(“日期”)
plt.ylabel(“溫度 (單位 ℃)”)
plt.title(“氣溫變化”)

#繪圖
plt.scatter(x_3, y_3, label=“3月”, color=“orange”, linewidths=1)
plt.scatter(x_10, y_10, label=“10月”, color=“blue”, linewidths=1)
plt.legend()

#加網格線
plt.grid()

plt.show()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章