18、气象学中风场的绘制

2024-03-21 08:30
文章标签 绘制 18 气象学 中风

本文主要是介绍18、气象学中风场的绘制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、批量读取数据
  • 二、绘制2022年的平均风场
  • 三、绘制每个季节的平均风场
  • 四、绘制每个月的风场

前言

数据及代码下载链接➡️:如何绘制自定义颜色的风场图

一、批量读取数据

import os
import xarray as xrfolder_path = "./"
file_pattern = os.path.join(folder_path, "*.nc")try:ds = xr.open_mfdataset(file_pattern)u10 = ds["u10"]v10 = ds["v10"]ds.close()except Exception as e:print(f"读取文件时出现错误:{str(e)}")

二、绘制2022年的平均风场

import xarray as xr
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.offsetbox import AnchoredText
from matplotlib.colors import ListedColormap
import cmaps
import matplotlib.ticker as ticker
from matplotlib.gridspec import GridSpec
from cartopy.mpl.geoaxes import GeoAxes
from mpl_toolkits.axes_grid1 import AxesGrid
import datetime as dt
# 求解纬向风、经向风以及风速的年平均值
u10_ana = u10[1:,:,:].mean(axis=0)
v10_ana = v10[1:,:,:].mean(axis=0)
windspeed_ana = np.sqrt(u10_ana**2+v10_ana**2)
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.gridspec import GridSpecfig = plt.figure(figsize=(6, 5), dpi=200)
gs = GridSpec(1, 1, figure=fig)
ax = fig.add_subplot(gs[0, 0], projection=ccrs.PlateCarree(central_longitude=180))leftlon, rightlon, lowerlat, upperlat = (-180, 180, -90, 90)  # 设置全球地图范围
img_extent = [leftlon, rightlon, lowerlat, upperlat]
lon = u10_ana.longitude
lat = u10_ana.latituded = ax.quiver(lon[::10], lat[::10], u10_ana[::10, ::10], v10_ana[::10, ::10], windspeed_ana[::10, ::10], color="k",)
ax.quiverkey(d, 0.95, 1.1, 5, '5 m/s', labelpos='S', coordinates='axes')
ax.add_feature(cfeature.BORDERS.with_scale('10m'))
ax.add_feature(cfeature.COASTLINE.with_scale('10m'))
ax.set_extent(img_extent, crs=ccrs.PlateCarree())  # 设置地图显示范围为全球ax.set_xticks([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180], crs=ccrs.PlateCarree())
ax.set_xticklabels(["180°W", "150°W", "120°W", "90°W", "60°W", "30°W", "0°", "30°E", "60°E", "90°E", "120°E", "150°E", "180°"],rotation=0, fontsize=6)
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
ax.set_yticklabels(["90°S", "60°S", "30°S", "0°", "30°N", "60°N", "90°N"], rotation=0, fontsize=6)ax.minorticks_on()
ax.xaxis.set_minor_locator(ticker.MultipleLocator(10))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(10))plt.show()

在这里插入图片描述

可以看到虽然绘制出了风场的图像,但是由于数据本身分辨率过高导致风场的图像并不是很直观,且风场的图标颜色不是很合适。因此,下面对风场进行微调,绘制出更加规范的图像。

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.gridspec import GridSpecfig = plt.figure(figsize=(6, 5), dpi=200)
gs = GridSpec(1, 1, figure=fig)
ax = fig.add_subplot(gs[0, 0], projection=ccrs.PlateCarree(central_longitude=180))leftlon, rightlon, lowerlat, upperlat = (-180, 180, -90, 90)  # 设置全球地图范围
img_extent = [leftlon, rightlon, lowerlat, upperlat]
lon = u10_ana.longitude
lat = u10_ana.latitudeax.quiver(lon[::20], lat[::20], u10_ana[::20, ::20], v10_ana[::20, ::20], windspeed_ana[::20, ::20], color="black",)
ax.quiverkey(d, 0.95, 1.1, 5, '5 m/s', labelpos='S', coordinates='axes')
ax.add_feature(cfeature.BORDERS.with_scale('10m'))
ax.add_feature(cfeature.COASTLINE.with_scale('10m'))
ax.set_extent(img_extent, crs=ccrs.PlateCarree())  # 设置地图显示范围为全球ax.set_xticks([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180], crs=ccrs.PlateCarree())
ax.set_xticklabels(["180°W", "150°W", "120°W", "90°W", "60°W", "30°W", "0°", "30°E", "60°E", "90°E", "120°E", "150°E", "180°"],rotation=0, fontsize=6)
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
ax.set_yticklabels(["90°S", "60°S", "30°S", "0°", "30°N", "60°N", "90°N"], rotation=0, fontsize=6)ax.minorticks_on()
ax.xaxis.set_minor_locator(ticker.MultipleLocator(10))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(10))plt.show()

在这里插入图片描述

可以看到当我们调整了ax.quiver(lon[::20], lat[::20], u10_ana[::20, ::20], v10_ana[::20, ::20], windspeed_ana[::20, ::20], color="black")语句之后,图片变得更加规范了,但是风场的颜色依旧没有发生变化,这个又该怎么办呢?
这里解释一下,虽然我们使用函数对箭头的颜色进行了设置,但是Matplotlib还是会根据风速的大小绘制不同颜色的箭头,而如果要绘制统一颜色或者自定义箭头的颜色就需要自定义色标,然后利用cmap这个参数了。下面,展示使用cmap参数将所有箭头设置为黑色。

自定义色标

newcolors = [[0.03921, 0.03921, 0.03529]]
newcmap_1 = ListedColormap(newcolors)
newcmap_1

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.gridspec import GridSpecfig = plt.figure(figsize=(6, 5), dpi=200)
gs = GridSpec(1, 1, figure=fig)
ax = fig.add_subplot(gs[0, 0], projection=ccrs.PlateCarree(central_longitude=180))leftlon, rightlon, lowerlat, upperlat = (-180, 180, -90, 90)  # 设置全球地图范围
img_extent = [leftlon, rightlon, lowerlat, upperlat]
lon = u10_ana.longitude
lat = u10_ana.latituded = ax.quiver(lon[::20], lat[::20], u10_ana[::20, ::20], v10_ana[::20, ::20], windspeed_ana[::20, ::20], cmap=newcmap_1)
ax.quiverkey(d,0.875, 1.025, 10, '10 m/s', labelpos='E', coordinates='axes', color="k") # 设置箭头示例颜色
ax.add_feature(cfeature.BORDERS.with_scale('10m'))
ax.add_feature(cfeature.COASTLINE.with_scale('10m'))
ax.set_extent(img_extent, crs=ccrs.PlateCarree())  # 设置地图显示范围为全球ax.set_xticks([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180], crs=ccrs.PlateCarree())
ax.set_xticklabels(["180°W", "150°W", "120°W", "90°W", "60°W", "30°W", "0°", "30°E", "60°E", "90°E", "120°E", "150°E", "180°"],rotation=0, fontsize=6)
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
ax.set_yticklabels(["90°S", "60°S", "30°S", "0°", "30°N", "60°N", "90°N"], rotation=0, fontsize=6)ax.minorticks_on()
ax.xaxis.set_minor_locator(ticker.MultipleLocator(10))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(10))
ax.set_title("自定义风场颜色(示例一)")
plt.show()

在这里插入图片描述

将箭头更改为其他颜色

newcolors = [[0.545098, 0.941176, 0.878431]]
newcmap_2 = ListedColormap(newcolors)newcmap_2

在这里插入图片描述

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.gridspec import GridSpecfig = plt.figure(figsize=(6, 5), dpi=200)
gs = GridSpec(1, 1, figure=fig)
ax = fig.add_subplot(gs[0, 0], projection=ccrs.PlateCarree(central_longitude=180))leftlon, rightlon, lowerlat, upperlat = (-180, 180, -90, 90)  # 设置全球地图范围
img_extent = [leftlon, rightlon, lowerlat, upperlat]
lon = u10_ana.longitude
lat = u10_ana.latituded = ax.quiver(lon[::20], lat[::20], u10_ana[::20, ::20], v10_ana[::20, ::20], windspeed_ana[::20, ::20], cmap=newcmap_2)
ax.quiverkey(d,0.875, 1.025, 10, '10 m/s', labelpos='E', coordinates='axes', color="k") # 设置箭头示例颜色
ax.add_feature(cfeature.BORDERS.with_scale('10m'))
ax.add_feature(cfeature.COASTLINE.with_scale('10m'))
ax.set_extent(img_extent, crs=ccrs.PlateCarree())  # 设置地图显示范围为全球ax.set_xticks([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180], crs=ccrs.PlateCarree())
ax.set_xticklabels(["180°W", "150°W", "120°W", "90°W", "60°W", "30°W", "0°", "30°E", "60°E", "90°E", "120°E", "150°E", "180°"],rotation=0, fontsize=6)
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
ax.set_yticklabels(["90°S", "60°S", "30°S", "0°", "30°N", "60°N", "90°N"], rotation=0, fontsize=6)ax.minorticks_on()
ax.xaxis.set_minor_locator(ticker.MultipleLocator(10))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(10))
ax.set_title("自定义风场颜色(示例二)")
plt.show()

在这里插入图片描述

三、绘制每个季节的平均风场

import xarray as xr
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.offsetbox import AnchoredText
from matplotlib.colors import ListedColormap
import cmaps
import matplotlib.ticker as ticker
from matplotlib.gridspec import GridSpec
from cartopy.mpl.geoaxes import GeoAxes
from mpl_toolkits.axes_grid1 import AxesGrid
import datetime as dtfig = plt.figure(layout="constrained", figsize=(12, 7), dpi=200)
gs = GridSpec(2, 2, figure=fig)proj = ccrs.PlateCarree(central_longitude=180)
leftlon, rightlon, lowerlat, upperlat = (-180, 180, -90, 90)
img_extent = [leftlon, rightlon, lowerlat, upperlat]lon = u10.longitude
lat = u10.latitudeseasons = [(0, 3, "DJF"), (3, 6, "MAM"), (6, 9, "JJA"), (9, 12, "SON")]for i, (start, end, title) in enumerate(seasons):ax = fig.add_subplot(gs[i // 2, i % 2], projection=ccrs.PlateCarree())u10_sea = u10[start:end, :, :].mean(axis=0)v10_sea = v10[start:end, :, :].mean(axis=0)windspeed_sea = np.sqrt(u10_sea**2+v10_sea**2)d = ax.quiver(lon[::20], lat[::20], u10_sea[::20, ::20], v10_sea[::20, ::20], windspeed_sea[::20, ::20], cmap=newcmap_1)ax.quiverkey(d, 0.875, 1.025, 10, '10 m/s', labelpos='E', coordinates='axes', color="k")ax.add_feature(cfeature.BORDERS.with_scale('10m'))ax.add_feature(cfeature.COASTLINE.with_scale('10m'))ax.set_extent(img_extent, crs=ccrs.PlateCarree())ax.set_xticks([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180], crs=ccrs.PlateCarree())ax.set_xticklabels(["180°W", "150°W", "120°W", "90°W", "60°W", "30°W", "0°", "30°E", "60°E", "90°E", "120°E", "150°E", "180°"],rotation=0, fontsize=6)ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())ax.set_yticklabels(["90°S", "60°S", "30°S", "0°", "30°N", "60°N", "90°N"], rotation=0, fontsize=6)ax.minorticks_on()ax.xaxis.set_minor_locator(ticker.MultipleLocator(10))ax.yaxis.set_minor_locator(ticker.MultipleLocator(10))ax.set_title(title)plt.tight_layout()
plt.show()

在这里插入图片描述

四、绘制每个月的风场

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.gridspec import GridSpecfig = plt.figure(figsize=(12, 12), dpi=200)
gs = GridSpec(4, 3, figure=fig)proj = ccrs.PlateCarree(central_longitude=180)
leftlon, rightlon, lowerlat, upperlat = (-180, 180, -90, 90)
img_extent = [leftlon, rightlon, lowerlat, upperlat]lon = u10.longitude
lat = u10.latitudemonths = list(zip(np.arange(12), ['Dec','Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov']))for i, (month, title) in enumerate(months):ax = fig.add_subplot(gs[i // 3, i % 3], projection=ccrs.PlateCarree())u10_mon = u10[month, :, :]v10_mon = v10[month, :, :]windspeed_mon = np.sqrt(u10_mon**2+v10_mon**2)d = ax.quiver(lon[::20], lat[::20], u10_mon[::20, ::20], v10_mon[::20, ::20], windspeed_mon[::20, ::20], cmap=newcmap_1)ax.quiverkey(d, 0.7, 1.1, 10, '10 m/s', labelpos='E', coordinates='axes', color="k", fontproperties={"size":7})ax.add_feature(cfeature.BORDERS.with_scale('10m'))ax.add_feature(cfeature.COASTLINE.with_scale('10m'))ax.set_extent(img_extent, crs=ccrs.PlateCarree())ax.set_xticks([-180, -150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180], crs=ccrs.PlateCarree())ax.set_xticklabels(["180°W", "150°W", "120°W", "90°W", "60°W", "30°W", "0°", "30°E", "60°E", "90°E", "120°E", "150°E", "180°"],rotation=0, fontsize=4)ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())ax.set_yticklabels(["90°S", "60°S", "30°S", "0°", "30°N", "60°N", "90°N"], rotation=0, fontsize=3)ax.minorticks_on()ax.xaxis.set_minor_locator(ticker.MultipleLocator(10))ax.yaxis.set_minor_locator(ticker.MultipleLocator(10))ax.set_title(title, fontsize=7)
# plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9)
# plt.tight_layout(pad=1.5)
plt.show()

在这里插入图片描述
数据及代码下载链接➡️:如何绘制自定义颜色的风场图

这篇关于18、气象学中风场的绘制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/832217

相关文章

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

使用Python绘制可爱的招财猫

《使用Python绘制可爱的招财猫》招财猫,也被称为“幸运猫”,是一种象征财富和好运的吉祥物,经常出现在亚洲文化的商店、餐厅和家庭中,今天,我将带你用Python和matplotlib库从零开始绘制一... 目录1. 为什么选择用 python 绘制?2. 绘图的基本概念3. 实现代码解析3.1 设置绘图画

Python绘制土地利用和土地覆盖类型图示例详解

《Python绘制土地利用和土地覆盖类型图示例详解》本文介绍了如何使用Python绘制土地利用和土地覆盖类型图,并提供了详细的代码示例,通过安装所需的库,准备地理数据,使用geopandas和matp... 目录一、所需库的安装二、数据准备三、绘制土地利用和土地覆盖类型图四、代码解释五、其他可视化形式1.

如何用Python绘制简易动态圣诞树

《如何用Python绘制简易动态圣诞树》这篇文章主要给大家介绍了关于如何用Python绘制简易动态圣诞树,文中讲解了如何通过编写代码来实现特定的效果,包括代码的编写技巧和效果的展示,需要的朋友可以参考... 目录代码:效果:总结 代码:import randomimport timefrom math

【WebGPU Unleashed】1.1 绘制三角形

一部2024新的WebGPU教程,作者Shi Yan。内容很好,翻译过来与大家共享,内容上会有改动,加上自己的理解。更多精彩内容尽在 dt.sim3d.cn ,关注公众号【sky的数孪技术】,技术交流、源码下载请添加微信号:digital_twin123 在 3D 渲染领域,三角形是最基本的绘制元素。在这里,我们将学习如何绘制单个三角形。接下来我们将制作一个简单的着色器来定义三角形内的像素

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

react笔记 8-18 事件 方法 定义方法 获取/改变数据 传值

1、定义方法并绑定 class News extends React.Component {constructor(props) {super(props)this.state = {msg:'home组件'}}run(){alert("我是一个run") //方法写在类中}render() {return (<div><h2>{this.state.msg}</h2><button onCli

YOLOv8/v10+DeepSORT多目标车辆跟踪(车辆检测/跟踪/车辆计数/测速/禁停区域/绘制进出线/绘制禁停区域/车道车辆统计)

01:YOLOv8 + DeepSort 车辆跟踪 该项目利用YOLOv8作为目标检测模型,DeepSort用于多目标跟踪。YOLOv8负责从视频帧中检测出车辆的位置,而DeepSort则负责关联这些检测结果,从而实现车辆的持续跟踪。这种组合使得系统能够在视频流中准确地识别并跟随特定车辆。 02:YOLOv8 + DeepSort 车辆跟踪 + 任意绘制进出线 在此基础上增加了用户

使用matplotlib绘制散点图、柱状图和饼状图-学习篇

一、散点图 Python代码如下: num_points = 100x = np.random.rand(num_points) #x点位随机y = np.random.rand(num_points) #y点位随机colors = np.random.rand(num_points) #颜色随机sizes = 1000 * np.random.rand(num_points) # 大