matplotlib繪製散點圖

假設通過爬蟲獲取到了北京2016年3,10月份每天白天的最高氣溫(分別位於列表y_3,y_10),要求繪製如下散點圖

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]

在這裏插入圖片描述

from matplotlib import pyplot as plt
from matplotlib import font_manager

my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/simfang.ttf")  # 加載本地windows中的字體
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]
x_3 = range(1, 32)
x_10 = range(51, 82)
# 設置圖片大小
plt.figure(figsize=(20, 8), dpi=80)  #寬 高 像素
# 繪圖
plt.scatter(x_3, y_3, label="3月份")
plt.scatter(x_10, y_10, label="10月份")  # label:圖中左上角的圖例
# 調整x軸的刻度
_x = list(x_3) + list(x_10)
_xtick_labels = ["3月{}日".format(i) for i in x_3]
_xtick_labels += ["10月{}日".format(i - 50) for i in x_10]
plt.xticks(_x[::3], _xtick_labels[::3], fontproperties=my_font, rotation=45)
# 設置圖例
plt.legend(loc="upper left", prop=my_font)
# 添加描述信息
plt.xlabel("時間", fontproperties=my_font)
plt.ylabel("溫度", fontproperties=my_font)
plt.title("標題", fontproperties=my_font)
# 保存圖片
plt.savefig("C:\\Users\\Administrator\\Desktop\\new.png")
# 展示
plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章