简单折线图
import matplotlib.pyplot as plt
import random
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
# 修改窗口大小
plt.figure(figsize=(20, 8), dpi=80)
# 调整x轴的刻度
_xtick_labels = ['10点{}分'.format(i) for i in range(60)]
_xtick_labels += ['11点{}分'.format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=270)
# 添加描述信息
plt.xlabel("时间") # x轴信息
plt.ylabel("温度 单位(℃)") # y轴信息
plt.title("10点到12点每分钟温度变化情况") # 图片标题
plt.plot(x, y)
plt.show()
显示中文字体
from matplotlib import font_manager
# win10系统,在左下角搜索字体可以看见本机里所有字体的位置
my_font = font_manager.FontProperties(fname="C:\\Windows\\Fonts\\MSYH.TTC")
import random
import matplotlib.pyplot as plt
from matplotlib import font_manager
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
# 添加中文字体
my_font = font_manager.FontProperties(fname="C:\\Windows\\Fonts\\MSYH.TTC")
# 修改窗口大小
plt.figure(figsize=(20, 8), dpi=80)
# 调整x轴的刻度
_xtick_labels = ['10点{}分'.format(i) for i in range(60)]
_xtick_labels += ['11点{}分'.format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(list(x)[::3], _xtick_labels[::3], rotation=270, fontproperties=my_font)
# 添加描述信息
plt.xlabel("时间", fontproperties=my_font) # x轴信息
plt.ylabel("温度 单位(℃)", fontproperties=my_font) # y轴信息
plt.title("10点到12点每分钟温度变化情况", fontproperties=my_font) # 图片标题
plt.plot(x, y)
plt.show()
中文有了,但也不是完全有了,加上下面代码就完全有了
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 第二行好像可加可不加,亲测效果都一样
plt.rcParams['axes.unicode_minus']=False