|
楼主 |
发表于 2024-10-19 19:47
|
显示全部楼层
1.绘制简单的温度折线图
①json数据
例如我们有某自动站2024年4月25日的如下数据:
[{"type":"A.01","Datetime":"2024/04/25 12","TEM":21.7},{"type":"A.01","Datetime":"2024/04/25 13","TEM":19.9},{"type":"A.01","Datetime":"2024/04/25 14","TEM":18.7},{"type":"A.01","Datetime":"2024/04/25 15","TEM":18.0},{"type":"A.01","Datetime":"2024/04/25 16","TEM":17.2},{"type":"A.01","Datetime":"2024/04/25 17","TEM":16.4},{"type":"A.01","Datetime":"2024/04/25 18","TEM":15.9},{"type":"A.01","Datetime":"2024/04/25 19","TEM":15.6},{"type":"A.01","Datetime":"2024/04/25 20","TEM":15.2},{"type":"A.01","Datetime":"2024/04/25 21","TEM":15.1},{"type":"A.01","Datetime":"2024/04/25 22","TEM":14.9},{"type":"A.01","Datetime":"2024/04/25 23","TEM":15.3},{"type":"A.01","Datetime":"2024/04/26 00","TEM":18.3},{"type":"A.01","Datetime":"2024/04/26 01","TEM":22.0},{"type":"A.01","Datetime":"2024/04/26 02","TEM":25.2},{"type":"A.01","Datetime":"2024/04/26 03","TEM":26.2},{"type":"A.01","Datetime":"2024/04/26 04","TEM":26.1},{"type":"A.01","Datetime":"2024/04/26 05","TEM":26.6},{"type":"A.01","Datetime":"2024/04/26 06","TEM":27.9},{"type":"A.01","Datetime":"2024/04/26 07","TEM":28.1},{"type":"A.01","Datetime":"2024/04/26 08","TEM":28.3},{"type":"A.01","Datetime":"2024/04/26 09","TEM":27.5},{"type":"A.01","Datetime":"2024/04/26 10","TEM":26.9},{"type":"A.01","Datetime":"2024/04/26 11","TEM":26.1}]
我们可以判断出是json格式的数据,并且里面有‘type’、‘Datetime’,‘TEM’三组数据。我们需要用‘Datetime’,‘TEM’两组数据。
我们主要用到matplotlib、json、pandas三个库。其中pandas不是必须使用的,因为pandas处理数据较方便,我们直接使用pandas库。
- import json #json库
- import pandas as pd #pandas库
- from datetime import datetime #时间处理库,因为通常我们获取到的数据时间格式为utc时间,需转换为btj更方便
- import matplotlib.pyplot as plt #matplotlib库
- from matplotlib import rcParams
- data = '[{"type":"A.01","Datetime":"2024/04/25 12","TEM":21.7},{"type":"A.01","Datetime":"2024/04/25 13","TEM":19.9},{"type":"A.01","Datetime":"2024/04/25 14","TEM":18.7},{"type":"A.01","Datetime":"2024/04/25 15","TEM":18.0},{"type":"A.01","Datetime":"2024/04/25 16","TEM":17.2},{"type":"A.01","Datetime":"2024/04/25 17","TEM":16.4},{"type":"A.01","Datetime":"2024/04/25 18","TEM":15.9},{"type":"A.01","Datetime":"2024/04/25 19","TEM":15.6},{"type":"A.01","Datetime":"2024/04/25 20","TEM":15.2},{"type":"A.01","Datetime":"2024/04/25 21","TEM":15.1},{"type":"A.01","Datetime":"2024/04/25 22","TEM":14.9},{"type":"A.01","Datetime":"2024/04/25 23","TEM":15.3},{"type":"A.01","Datetime":"2024/04/26 00","TEM":18.3},{"type":"A.01","Datetime":"2024/04/26 01","TEM":22.0},{"type":"A.01","Datetime":"2024/04/26 02","TEM":25.2},{"type":"A.01","Datetime":"2024/04/26 03","TEM":26.2},{"type":"A.01","Datetime":"2024/04/26 04","TEM":26.1},{"type":"A.01","Datetime":"2024/04/26 05","TEM":26.6},{"type":"A.01","Datetime":"2024/04/26 06","TEM":27.9},{"type":"A.01","Datetime":"2024/04/26 07","TEM":28.1},{"type":"A.01","Datetime":"2024/04/26 08","TEM":28.3},{"type":"A.01","Datetime":"2024/04/26 09","TEM":27.5},{"type":"A.01","Datetime":"2024/04/26 10","TEM":26.9},{"type":"A.01","Datetime":"2024/04/26 11","TEM":26.1}]'
- j = json.loads(data) #读取json数据
- df = pd.DataFrame(j) #通过pandas库转为dataframe格式
- df['times'] = pd.to_datetime(df['Datetime'],format='%Y/%m/%d %H')+pd.Timedelta(hours = 8) #通过pandas中的时间处理功能转为北京时间,其中format='%Y/%m/%d %H'必须与数据中的时间格式一样。若源数据已经是北京时间,则不需要此代码。
- df['times'] = df['times'].apply(lambda x: datetime.strftime(x,"%H")) #只保留小时,后面用作x轴坐标。若想显示完全的日期时间则不需要此代码。
- x1 = df['times'] #横坐标即x
- y1 = df['TEM'] #纵坐标即y
- fig,ax1 = plt.subplots(figsize=(15,9)) #创建一个15*9的画布,也可用dpi=xx,数字越大越清晰,文件越大,出图也越慢
- plt.subplots_adjust(left=0.038, bottom=0.05, right=0.945, top=0.88, wspace=0.2, hspace=0.2) #子图ax距画布上下左右的间距
- config = {"font.sans-serif":'SimHei',"font.size": 12,"axes.unicode_minus":False}
- rcParams.update(config) #全局文字格式,没有此代码汉字会显示黑色的框框
- ax1.plot(x1,y1,c='r', linewidth = 1) #绘制折线,c是颜色,linewidth是折线粗细,
- for a, b in zip(x1,y1):
- ax1.text(a, b, b, ha='center', va='bottom', fontsize=15) #显示数值
- ax1.grid(True, linestyle="--",alpha=1,axis='x') #横坐标显示网格线,虚线,alpha是透明度
- plt.suptitle('24h温度图',x=0.45,y=0.98,ha='left',fontsize=25,color='red') #显示标题,注意这儿是显示的plt画布的标题,还可以显示ax的标题,用法为ax.settitle()
- plt.show() #显示图片
- #若要保存图片使用plt.savefig(),括号内为路径,例如plt.savefig("D:/tempchart.png")
复制代码
②csv格式:
前面的附件中有一个探空图文件,只需要将
- j = json.loads(data) #读取json数据
- df = pd.DataFrame(j) #通过pandas库转为dataframe格式
复制代码
换成
- df = pd.read_csv("2023030700-56187.csv")
复制代码
,再将X1,Y1中的表头改一下就行了。
③txt格式
我们读取历史数据时经常会用到cma的历史数据集,例如有一个数据集:
我们可以直接用pandas当作csv读取。
2.绘制雨量柱状图
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|