自学气象人 发表于 2024-3-6 20:40:24

一行命令画4种风羽(短线、长线、空心三角形、实心三角形)


问题
我们熟知的风羽有四种:短线代表风速2m/s,长线代表风速4m/s,空心三角代表风速20m/s,实心三角代表风速50m/s。但matplotlib里面只有短线、长线、三角形三种,尽管可以通过设置flagcolor=none,barcolor=balck将三角形变为空心,但也只支持3个level,且无法同时出现空心三角形和实心三角形,无法满足部分同学的需求。

解决思路
我在参考网上的一些资料后,基于原有的quiver绘图code做了些改动,增加了一个level,并将最高级别的level设置为实心三角形。这样的话,就可以结合matplotlib中原有的短线、长线、以及通过设置color实现的空心三角形构成4种风羽,从而满足需求了。

使用方法
step1 下载code
若有需求可在微信公众号自学气象人后台寻得修改后的code。

step2 替换原有code
将下载下来的quiver.py替换掉原有的quiver.py,我原有的quiver.py是在F:\software\anaconda3\Lib\site-packages\matplotlib路径下,大家根据自己实际情况对路径进行调整。

step3 正常使用quiver函数即可
注意,我提供的code里面已经默认分了4个level:短线代表风速2m/s,长线代表风速4m/s,空心三角代表风速20m/s,实心三角代表风速50m/s。如果与自己需求一致,则不用修改;如果不一致,可以通过barb_increments参数修改,下面会放示例。

代码示例
示例1
绘制一个80m/s的风羽,要求短线代表风速2m/s,长线代表风速4m/s,空心三角代表风速20m/s,实心三角代表风速50m/s。这里与默认level相同,无需修改。
import matplotlib.pyplot as plt

fig=plt.figure()
ax=fig.add_subplot(111);
ax.axis([-1,1,-1,1])
ax.set_xticks([])
ax.set_yticks([])
ax.barbs(0,0,30*1.6,40*1.6,length=8,linewidth=0.5,flagcolor='none',barbcolor='black')

plt.show()

示例2
绘制一个50m/s的风羽,要求短线代表风速2m/s,长线代表风速4m/s,空心三角代表风速20m/s。注意,这里没有用到第四个level,需要设置fullflag=None。如果不设置的话,默认还是fullflag=50。
import matplotlib.pyplot as plt

fig=plt.figure()
ax=fig.add_subplot(111);
ax.axis([-1,1,-1,1])
ax.set_xticks([])
ax.set_yticks([])
ax.barbs(0,0,30*1.,40*1.,length=8,linewidth=0.5,flagcolor='none',barbcolor='black',barb_increments=dict(half=2, full=4, flag=20,fullflag=None))

plt.show()

示例3
绘制一个50m/s的风羽,要求短线代表风速2m/s,长线代表风速4m/s,实心三角代表风速20m/s。注意,这里没有用到第四个level,需要设置fullflag=None。如果不设置的话,默认还是fullflag=50。
import matplotlib.pyplot as plt

fig=plt.figure()
ax=fig.add_subplot(111);
ax.axis([-1,1,-1,1])
ax.set_xticks([])
ax.set_yticks([])
ax.barbs(0,0,30*1.,40*1.,length=8,linewidth=0.5,barb_increments=dict(half=2, full=4, flag=20,fullflag=None))

plt.show()

示例4
花哨玩法:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 5)
X, Y = np.meshgrid(x, x)
U, V = 12*X, 12*Y
data = [(-1.5, .5, -6, -6),(1, -1, -46, 46),(-3, -1, 11, -11),(1, 1.5, 80, 80),(0.5, 0.25, 25, 15),(-1.5, -0.5, -5, 40)]
data = np.array(data, dtype=[('x', np.float32), ('y', np.float32), ('u', np.float32), ('v', np.float32)])
# Default parameters, uniform grid
ax = plt.subplot(2, 2, 1)
ax.barbs(X, Y, U, V)
# Arbitrary set of vectors, make them longer and change the pivot point
#(point around which they're rotated) to be the middle
ax = plt.subplot(2, 2, 2)
ax.barbs(data['x'], data['y'], data['u'], data['v'], length=8, pivot='middle')
# Showing colormapping with uniform grid. Fill the circle for an empty barb,
# don't round the values, and change some of the size parameters
ax = plt.subplot(2, 2, 3)
ax.barbs(X, Y, U, V, np.sqrt(U*U + V*V), fill_empty=True, rounding=False,sizes=dict(emptybarb=0.25, spacing=0.2, height=0.3))
# Change colors as well as the increments for parts of the barbs
ax = plt.subplot(2, 2, 4)
ax.barbs(data['x'], data['y'], data['u'], data['v'], flagcolor='r',barbcolor=['b', 'g'], barb_increments=dict(half=10, full=20, flag=100,fullflag=200))
plt.show()



参考:

• https://www.cnblogs.com/kallan/p/6279932.html

• https://matplotlib.org/stable/api/_as_gen/matplotlib.quiver.Barbs.html#matplotlib.quiver.Barbs



文章来源于微信公众号:自学气象人
页: [1]
查看完整版本: 一行命令画4种风羽(短线、长线、空心三角形、实心三角形)