运行环境 : python 3.6.0
用 Matplotlib 的 scatter 方法绘制散点图 , 绘制温度分布 , 数据来源于 http://lishi.tianqi.com/beiling/index.html
核心代码 :
# -*- encoding: utf-8 -*-
# @Version : Python 3.6.0
# @Function : 本代码为利用 Matplotlib 库 画散点图
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties # 字体属性设置
font = FontProperties(fname=r"C:\windows\fonts\simsun.ttc", size=14) # 使用本地字体(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)
# 使用scanner方法绘制散点图 , 和之前绘制折线图的唯一区别
plt.scatter(x_3, y_3, label="三月份")
plt.scatter(x_10, y_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='SimHei', rotation=45)
# 添加图例
plt.legend(loc='upper left', prop=font)
plt.xlabel("时间",fontproperties='SimHei', size=16)
plt.ylabel("温度",fontproperties='SimHei', rotation=90, size=16)
plt.title("标题",fontproperties='SimHei', size=18)
plt.show() # 展示
运行结果展示 :