Line graph of temperature change from 11 o'clock to 12 o'clock in a city

Requirement: draw a line chart of temperature change every minute from 11 o'clock to 12 o'clock in a city, the temperature range is 15 degrees to 18 degrees

import matplotlib.pyplot as plt
import random

# 画出温度变化图

# 0.准备x, y坐标的数据
x = range(60)
y_shanghai = [random.uniform(15, 18) for i in x]
y_beijing = [random.uniform(20, 25) for i in x]

# 1.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 2.绘制折线图
plt.plot(x, y_shanghai,label="上海")
plt.plot(x, y_beijing,linestyle='--',color='c',label="北京")

# 显示图例 默认是0,或者best
plt.legend(loc=8)

# 构造x轴刻度标签
x_ticks_label = ["11点{}分".format(i) for i in x]

# 构造y轴刻度
y_ticks = range(40)

# 修改x,y轴坐标的刻度显示
plt.xticks(x[::5], x_ticks_label[::5])
plt.yticks(y_ticks[::5])
# plt.xticks(x_ticks_label[::5])  #必须最开始传递进去的是数字

#添加网格线 True是添加,-是实线,--是虚线,alpha对应的透明度 linestyle绘制网格的方式
plt.grid(True, linestyle='--', alpha=0.2)

#添加描述信息
plt.xlabel("时间")
plt.ylabel("温度")
plt.title("中午11点--12点某城市温度变化图", fontsize=20)


# 3.显示图像
plt.show()

 

Note: plt.show() will release the figure resource. If you save the picture after displaying the image, you will only be able to save an empty picture.

 

Guess you like

Origin blog.csdn.net/weixin_48135624/article/details/115314695