爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2099|回复: 13

[求助] python绘制风场风羽图时2m/s以下的如何显示风杆而不是圆圈?

[复制链接]
发表于 2024-10-31 15:36:13 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
各位大佬,请问使用python中的barbs()函数绘制低层风场时,如何设置小于2m/s的风显示风向(风杆)?已尝试(1)将for index, angle in np.ndenumerate(angles):后第一个if模块删除、(2)在if half_barb[index]:后同级增加if empty_flag[index] and u[index] != 0 and v[index] != 0:模块,但是还是显示有圆圈,求助各位大佬。

还是有圆圈

还是有圆圈

增加模块

增加模块

注释掉模块

注释掉模块

密码修改失败请联系微信:mofangbao
发表于 2024-11-3 10:29:26 | 显示全部楼层
本帖最后由 edwardli 于 2024-11-3 20:03 编辑

楼上正解是出自2021版《Python气象资料处理与可视化》(河北科技出版社)。以下为“Python语言在气象业务中的应用系列课程”课件内容:
matplotlib设计上对于不足半个风羽half,也就是2m/s的风场不做绘制或以空心圆替代,但是同时该点的风向信息就被消隐了。
一、不想要空心圆,,不关心风向
  • ax_barbs(size=dict(emptybarb=0)就不显示小圆圈。radius of the circle used for low magnitudes
  • fill_empty绘制的空barb(圆)是否应填充旗帜颜色。如果未填充,则中心是透明的
二、不想要空心圆,,关心风向
  • 旧版:可以通过修改matplotlib自带的barbs方法,将风场的风,向表示出来(见 《Python气象资料处理与可视化》5.4.1节)
  • 新版【3.4.2、3.9.X】:第【1】步:D:\Anaconda3\Libsite-packages\matplotlib\quiver.py 第1061行条件由
  1. if empty_flag[index]:
复制代码

改为
           
  1. if empty_flag[index] and u[index] == 0 and v[index] == 0:
复制代码


第【2】步:在大概第1110行附近,加入代码:
  1.             if empty_flag[index] and  u[index] != 0 and v[index] != 0:
  2.                 # If the half barb is the first on the staff, traditionally it
  3.                 # is offset from the end to make it easy to distinguish from a
  4.                 # barb with a full one
  5.                 if offset == length:
  6.                     poly_verts.append((endx, endy + offset))
  7.                     offset -= 1.5 * spacing
  8.                 poly_verts.extend(
  9.                     [(endx, endy + offset),
  10.                      (endx + full_height / 200, endy + offset + full_width / 40),
  11.                      (endx, endy + offset)])
复制代码



第【3】步:删余_pycache_文件夹,重启内核


quiver.py (45.84 KB, 下载次数: 11)
密码修改失败请联系微信:mofangbao
回复 支持 2 反对 0

使用道具 举报

发表于 2024-11-4 15:24:53 | 显示全部楼层
提供一个不用修改 matplotlib 源码的函数版:
  1. def custom_barbs(ax, *args, **kwargs):
  2.     '''修改后的 Axes.barbs,使小于 half 的风速显示风杆而非圆圈。'''
  3.     from matplotlib.path import Path
  4.     from matplotlib.quiver import Barbs

  5.     out_list = []

  6.     def decorator(method):
  7.         def wrapper(self, *args, **kwargs):
  8.             mag = args[0]
  9.             n_flags, n_barb, half_flag, empty_flag = method(self, *args, **kwargs)
  10.             small_flag = empty_flag & (mag > 0)
  11.             out_list.append(small_flag)
  12.             empty_flag = mag == 0
  13.             half_flag[small_flag] = True
  14.             return n_flags, n_barb, half_flag, empty_flag

  15.         return wrapper

  16.     try:
  17.         method = Barbs._find_tails
  18.         Barbs._find_tails = decorator(method)
  19.         barbs = ax.barbs(*args, **kwargs)
  20.     finally:
  21.         Barbs._find_tails = method

  22.     small_flag = out_list[0]
  23.     for i in np.nonzero(small_flag)[0]:
  24.         verts = barbs._paths[i].vertices[:2]
  25.         verts = np.vstack((verts, verts[0]))
  26.         barbs._paths[i] = Path(verts)

  27.     return barbs
复制代码
这样一来就能在一张图里同时实现带圆圈和不带圆圈的效果:
  1. axes[0].barbs(lon, lat, u10, v10, regrid_shape=8, transform=crs)
  2. custom_barbs(axes[1], lon, lat, u10, v10, regrid_shape=8, transform=crs)
复制代码
test.png
不过这个做法仅在 matplotlib 3.9 里测试过,更低的版本不确定能不能行。
无圆圈风向杆.ipynb (130.9 KB, 下载次数: 2)
密码修改失败请联系微信:mofangbao
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2024-10-31 15:39:38 | 显示全部楼层
我的matplotlib版本是3.5.1
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

发表于 2024-11-1 10:35:39 | 显示全部楼层
plt.barbs(lon,lat,u,v,barb_increments={'half':2,'full':4,'flag':20})

barb_increments={‘half’:2,‘full’:4,‘flag’:20},修改风矢杆长短杆线和三角分别代表的风速大小。

密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-1 14:47:38 | 显示全部楼层
remnant 发表于 2024-11-1 10:35
plt.barbs(lon,lat,u,v,barb_increments={'half':2,'full':4,'flag':20})

barb_increments={‘half’:2 ...

您好,barb_increments={'half':2,'full':4,'flag':20}我已经设置了,短划线为2m/s,但是小于2m/s的显示成圆圈了,请问如何把圆圈变为有风向信息的风杆?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

发表于 2024-11-2 09:01:18 | 显示全部楼层
图片供参考
Snipaste_2024-11-02_08-56-51.png
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

发表于 2024-11-3 17:32:01 | 显示全部楼层
楼主的matplotlib版本是多少?
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

发表于 2024-11-3 20:12:48 | 显示全部楼层
雷小Py-20:讨厌的小风速圆圈圈_2024.11.04 https://www.bilibili.com/video/B ... id_from=333.999.0.0
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-4 12:50:27 | 显示全部楼层
谢谢大家,成功解决啦!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-4 12:52:57 | 显示全部楼层
edwardli 发表于 2024-11-3 10:29
楼上正解是出自2021版《Python气象资料处理与可视化》(河北科技出版社)。以下为“Python语言在气象业务中 ...

谢谢大佬,完美解决啦!!!
密码修改失败请联系微信:mofangbao
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表