Matplotlib学习笔记【02】
"""
Matplotlib Part 2
#1 自定义坐标轴刻度 plt.xticks()
#2 中文显示的设置方法
#3 添加坐标轴标签与图表名称 plt.xlabel(); plt.title()
#4 绘制网格 plt.grid()
#5 添加图例 plt.legent()
"""
import matplotlib.pyplot as plt
import random
from matplotlib import font_manager
my_font = font_manager.FontProperties(fname="C:\Windows\Fonts\simsun.ttc", size=10)
"""
font_manager是Python包matplotlib之中自带的一个module,
FontProperties是该模块中的一个class
参数fname:需要传入windows系统中存放字体的文件路径
"""
x = range(0, 60)
y_1 = [random.randint(20, 35) for i in range(60)]
y_2 = [random.randint(15, 30) for i in range(60)]
plt.figure(figsize=(16, 7), dpi=160, edgecolor="red", )
x_label = ["10点{}分".format(i) for i in range(60)]
plt.xticks(x, x_label, rotation=45, fontproperties=my_font)
plt.xlabel("时间", fontproperties=my_font)
plt.ylabel("温度 单位(℃)", fontproperties=my_font)
plt.title("温\湿度信息", fontproperties=my_font, size=20)
plt.plot(x, y_1, linestyle='--', label="温度 单位:℃")
plt.plot(x, y_2, color='orange', label="湿度 单位:$ml / m^3$")
"""
- 需要绘制多条图线的话,多次plot就行了
- 线条属性
- color='red' # 颜色
- linestyle='--' # 线条样式
- linewidth=5 # 线条宽度
- alpha=0.4 # 透明度
"""
plt.legend(prop=my_font, loc="lower right")
plt.grid(alpha=0.4)
plt.savefig("./温湿度.png")
plt.show()