最近用matplotlib绘制了一张天气折线图,分享给大家

#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@作者:志在星空
@时间: 2022/04/04/19:38
@文件名: 绘制折线图.py
@软件:PyCharm
"""

import matplotlib.pylab as pyl
import matplotlib.pyplot as plt

pyl.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
pyl.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

plt.figure(figsize=(20, 8), dpi=100)
x= range(1,32)

x_ticks_label = ["{}号".format(i) for i in x]
y_ticks=range(33)
h = [26, 21, 26, 26, 22, 20, 17, 19, 22, 28, 30, 28, 24,
     28, 25, 26, 25, 26, 25, 23, 24, 30, 32, 31, 30, 27, 26, 27, 29, 25, 25]
l = [17, 13, 17, 18, 18, 17, 14, 15, 16, 18, 19, 20, 18, 18,
     20, 20, 20, 20, 20, 16, 17, 19, 21, 24, 24, 23, 20, 18, 19, 18, 19]
pyl.title('武汉3月份气温走势',fontsize='20')
pyl.xlabel('日期',fontsize='15')# 刻度显示
plt.xticks(x[::1], x_ticks_label[::1])
plt.yticks(y_ticks[::1])
# 添加网格显示
plt.grid(True, linestyle="--", alpha=0.5)

pyl.ylabel('温度℃',fontsize='15')
plt.plot(x, h, color='r',marker='o', linestyle='--', label="高温")
plt.plot(x, l, color='b',marker='o', linestyle='-', label="低温")

plt.legend()
pyl.xlim(0,32)
pyl.ylim(0,32)
pyl.show()

好的,下面是使用matplotlib绘制两天内同一时刻天气温度随时间变化的折线图的代码示例: ```python import matplotlib.pyplot as plt import datetime # 生成两天内每小时的时间列表 start_time = datetime.datetime(2022, 11, 1, 0) end_time = datetime.datetime(2022, 11, 3, 0) delta = datetime.timedelta(hours=1) times = [] while start_time < end_time: times.append(start_time) start_time += delta # 模拟温度数据 temperatures = [23, 22, 21, 20, 19, 18, 17, 17, 16, 16, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 23, 23, 23, 22, 22, 21, 20, 20, 19, 18, 18, 17, 16, 16, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 23, 23, 23, 22, 22, 21, 20, 20, 19, 18, 18, 17, 16, 16, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 23, 23, 23, 22, 22, 21, 20, 20, 19, 18, 18, 17, 16, 16, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 23, 23, 23, 22, 22, 21, 20, 20, 19, 18, 18, 17, 16, 16, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] # 绘制折线图 plt.plot(times, temperatures) # 设置标题和轴标签 plt.title('Temperature over time') plt.xlabel('Time') plt.ylabel('Temperature (℃)') # 设置x轴的时间格式 plt.gcf().autofmt_xdate() # 展示图像 plt.show() ``` 这段代码会生成一个两天内同一时刻天气温度随时间变化的折线图,x轴是时间,y轴是温度。你可以将模拟的温度数据替换为真实的数据来绘制你想要的折线图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值