import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import random
# 设置字体
f = "/System/Library/Fonts/PingFang.ttc"
prop = fm.FontProperties(fname=f)
plt.rcParams['axes.unicode_minus']=False
# 生成数据
x = list(range(60))
y_beijing = [random.uniform(10,15) for i in x]
y_shanghai = [random.uniform(15,25) for i in x]
# 1. 创建画布
plt.figure(figsize=(20,8), dpi=100)
# 2.图形绘制
plt.plot(x,y_beijing, label="BeiJing", color="g", linestyle="-.")
plt.plot(x,y_shanghai, label="ShangHai")
# 2.1 添加x, y轴刻度
y_ticks = range(40)
plt.yticks(y_ticks[::5],fontproperties=prop)
x_ticks_label = ["11点{}分".format(i) for i in x]
plt.xticks(x[::5], x_ticks_label[::5],fontproperties=prop)
# 2.2 添加网格
plt.grid(True, linestyle="--", alpha=0.2)
# 2.3 添加描述信息
plt.xlabel("时间",fontproperties=prop)
plt.ylabel("温度",fontproperties=prop)
plt.title("一小时温度变化图",fontproperties=prop)
# 2.4 显示图例
plt.legend(loc="best")
# 3. 图像展示
plt.show()
生成的图片样式: