赞
踩
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
- import random
-
- from matplotlib import pyplot as plt
- from matplotlib import font_manager
-
- # 设置中文字体
- my_font = font_manager.FontProperties(fname="C:\\Windows\\Fonts\\msyh.ttc")
-
- # 指定x轴 从那里开始,到那里结束,步长是多少 x的个数和y的个数要一致,才能形成相同数量个点
- x = range(0, 120)
-
- # 这里的数字个数和x轴是一样的
- y = [random.randint(30, 45) for i in range(120)]
-
- # 设置图片大小
- plt.figure(figsize=(20, 8), dpi=80)
-
- # 绘图
- plt.plot(x, y)
-
- # 设置x轴的刻度 主要是调整步长
- # 正常步长
- # plt.xticks(range(2, 25))
-
- # 刻度变稀疏
- _xtick = list(x)
-
- # 刻度变稀疏,增大步长
- # _xtick = [i/2 for i in range(2, 50, 2)]
-
- # x轴刻度格式化输出
- _xtick_labels = ["15点{}分".format(i) for i in range(60)]
- _xtick_labels += ["16点{}分".format(i-60) for i in range(60, 120)]
-
- # 设置刻度, 还可以使用_xtick[::3]调整步长, rotation设置旋转90度放置重叠
- plt.xticks(_xtick[::3], _xtick_labels[::3], rotation=45, fontproperties=my_font)
-
- # 设置y轴刻度
- # plt.yticks(range(min(y), max(y) + 1))
-
- # 设置X轴标签
- plt.xlabel("时间", fontproperties=my_font)
- # 设置y轴标签
- plt.ylabel("温度 单位(℃)", fontproperties=my_font)
- # 设置图形标题
- plt.title("重庆15点-16点每分钟气温变化图", fontproperties=my_font)
-
- # 保存图片
- plt.savefig("./plot_01.png")
-
- # 显示图形
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。