python+气象 | 在地图背景下绘制全国站点气温分布图

0.写在前面

        本来想画含等温线+填色+标注站点降温的图的,但是因为要来的数据是文本文件,绘制等温图不是小白能干的过于麻烦,于是最后只画了地图+站点。

结果如图(save之后的png格式图片,python输出figure时南海部分在地图外)

84ae376a5d7348b1ab50a437acd82e52.png

1.用到的包

import matplotlib.pyplot as plt
import numpy as np
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import pandas as pd
from cartopy.io import shapereader as shpreader

包括matplotlib、numpy、cartopy、pandas。

使用conda下载包有三种格式(学艺不精,我经常混用,哪个能下就用哪个):

①.conda install 包  /  ②.pip install 包  /  ③.conda install -c conda-forge 包

2.数据

①excel格式的含站点经纬度温度的文件

②中国边界线(带省界和南海)的shp文件

3.代码

#读取数据pd.read_excel(excel格式的文件似乎不方便用contourf作图)
df = pd.read_excel(r"20200901 经纬温水.xlsx")
df.replace(999999, 0, inplace=True)
#上一步replace是把数据中的999999换成0
#999999表示该站点无此数据,因此做出的图白色的包括无站点和无数据两种情况
lat = df['Lat']
lon = df['Lon']
temps = df['TEM_Avg']

# 建立画布
fig2 = plt.figure(figsize=(15, 15))
proj = ccrs.PlateCarree(central_longitude=105)
leftlon, rightlon, lowerlat, upperlat = (70, 140, 15, 55) 
# 根据上下限确定范围,至少为10°

# 绘制地图
f2_ax1 = fig2.add_axes([0.1, 0.1, 0.8, 0.4], projection=proj)
# 在画布的绝对坐标建立子图
f2_ax1.set_extent([leftlon, rightlon, lowerlat, upperlat],
                  crs=ccrs.PlateCarree())

# 海岸线,50m精度
f2_ax1.add_feature(cfeature.COASTLINE.with_scale('50m'))

# 湖泊数据(但是这个貌似只画了比较大的湖泊,比如贝湖巴湖)
f2_ax1.add_feature(cfeature.LAKES, alpha=0.5) #alpha表示透明度

# 以下6条语句是定义地理坐标标签格式
f2_ax1.set_xticks(np.arange(leftlon, rightlon+10, 10), crs=ccrs.PlateCarree())
#rightlon或是下面的upperlat应写大一个间隔,在这里就是+10
f2_ax1.set_yticks(np.arange(lowerlat, upperlat+10, 10), crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter()
lat_formatter = LatitudeFormatter()
f2_ax1.xaxis.set_major_formatter(lon_formatter)
f2_ax1.yaxis.set_major_formatter(lat_formatter)
f2_ax1.set_title('20200901 China average temperature',
                 loc='center', fontsize=15)#定义名称,名称含中文出图时为空格,尚未解决

# 读取shp文件
china = shpreader.Reader(r"bou2_4l.dbf").geometries()

# 绘制中国国界省界九段线等等
f2_ax1.add_geometries(china, ccrs.PlateCarree(),
                      facecolor='none', edgecolor='black', zorder=1)

# 添加南海,实际上就是新建一个子图覆盖在之前子图的右下角
f2_ax2 = fig2.add_axes([0.7575, 0.0935, 0.08, 0.13], projection=proj)
f2_ax2.set_extent([105, 125, 0, 25], crs=ccrs.PlateCarree())
f2_ax2.add_feature(cfeature.COASTLINE.with_scale('50m'))
china = shpreader.Reader(r"bou2_4l.dbf").geometries()
f2_ax2.add_geometries(china, ccrs.PlateCarree(),
                      facecolor='none', edgecolor='black', zorder=1)


#绘制站点,颜色表示温度,cmap是选择一种colorbar的格式
f1 = f2_ax1.scatter(lon, lat, s=2, c=temps, cmap='afmhot_r',
                    transform=ccrs.PlateCarree())
f2 = f2_ax2.scatter(lon, lat, s=2, c=temps, cmap='afmhot_r',
                    transform=ccrs.PlateCarree())

#下面定义colorbar大小和位置
# colorbar 左 下 宽 高
l = 0.92
b = 0.1
w = 0.015
h = 0.4

# 对应 l,b,w,h;设置colorbar位置;
rect = [l, b, w, h]
cbar_ax = fig2.add_axes(rect)
cb = plt.colorbar(f2, cax=cbar_ax)

#图片保存
plt.savefig(r'average temperature.png', dpi=300) #dpi越大越清晰
#要先save在show,不然show出来的是空白图片
plt.show()

4.一些解释和参考

①dpi可以定义在创建画布那一步

②fig.add_axes参数可参考 add_axes()——python绘图_机尾云拉长的博客-CSDN博客_add_axes

③之前问师兄说应该可以用tricontourf画一个等温图,还没试,附一个官网的参考链接

Contour plot of irregularly spaced data — Matplotlib 3.5.2 documentation

④bou2_4l.dbf文件应与shp、shx格式文件在同一个文件夹,否则会报错

⑤cartopy中的投影格式可参考(一)Cartopy中的地图投影 - 气象学人 - 博客园

⑥catter基本介绍参考Python中scatter函数参数详解_幸运六叶草的博客-CSDN博客_python scatter参数详解

scatter函数中cmap(即colormap)参考官网color example code: colormaps_reference.py — Matplotlib 2.0.2 documentation

如果是统一的颜色,参考Python-画图(散点图scatter、保存savefig)及颜色大全_天才的汉堡叔叔的博客-CSDN博客_python散点图

⑦所有文件地址建议用完整地址

⑧本文主要参考滑动验证页面

设置colorbar参考(9条消息) matplotlib 合理设置colorbar和子图的对应关系_fangzuliang的博客-CSDN博客_matplotlib 子图colorbar

nc文件绘制等温线可以参考Python绘制气象实用地图(附代码和测试数据) | Climate2Weather

                                       和Python气象绘图笔记(四)——填色与colorbar - 知乎 (zhihu.com)

5.文件分享(bou2_4l)

链接:https://pan.baidu.com/s/1C-lQdy9nVQ7bMMQ0ulRuCA?pwd=1014 
提取码:1014

ps.站点数据需要自己获取哦,或者向自己的学长学姐要数据。

本人能力有限,尚在学习过程中。若有错误,敬请指正。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值