使用matplotlib轮廓/轮廓处理循环数据 [英] Handling cyclic data with matplotlib contour/contourf

查看:130
本文介绍了使用matplotlib轮廓/轮廓处理循环数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建风向的轮廓/轮廓图-问题是0 / 360deg不连续性对这两个函数都造成了破坏-试图对间隙进行插值并用 all 中间值(请参见下文)。我尝试了各种插值/移位方法,但没有结果。有没有人知道如何解决此问题?

I'm trying to create a contour/contourf plot of wind heading - the problem being that the 0/360deg discontinuity is playing havoc with both functions - trying to interpolate the gap and fill it with all the intervening values (see below). I've tried various interpolation/shifting ideas but nothing has come to fruition. Has anyone got any ideas about how to fix this?

一个最小工作代码示例:

A minimal-working-code example:

levels=np.array([1000.,975.,950.,925.,900.,875.,850.,825.,800.,775.,750.,700.,650.,600.,
550.,500.,450.,400.,350.,300.,250.,225.,200.,175.,150.])
arr = np.load("arr.npy")

fig = plt.figure(figsize=(6,10))
ax = plt.subplot(111)
clevs = np.arange(-360.,360.,45.)
clevs1 = np.linspace(np.min(arr),np.max(arr),100.)

cs = plt.contour(lons,levels,arr,clevs,colors = 'k')
for c in cs.collections: c.set_linestyle('solid')
ax.set_xlabel("Longitude")
ax.set_ylabel("Pressure Level (hPa)")
ax.set_yscale("log")
plt.gca().invert_yaxis()
ax.set_yticks(levels[::2])
ax.set_yticklabels(levels[::2].astype(int))
cs1 = plt.contourf(lons,levels,arr,clevs1,cmap=plt.cm.hsv)
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("bottom", "4%", pad="8.5%")
cbar = plt.colorbar(cs1, orientation="horizontal", cax = cax)
cbar.set_ticks(clevs[::1])
cbar.set_label(r"Wind Heading")

plt.clabel(cs, inline = 1, fontsize = 18, fmt = '%1.f', manual = True)
plt.tight_layout()
plt.show()

数据此处

推荐答案

想象一下如何对这样的数据集进行插值:除非您愿意,否则您不可能连续地从360°下方移动到0°。 unwrap 这些值(请参见 np.unwrap ),以便将接近0°的值重新解释为相同的+ 360°值。但是随后您又增加了所有轮廓,最终得到了接近2x360°的轮廓线,然后又是另一个边缘。

Imagine how you might possibly interpolate such a dataset: there's no way you could continuously move from just below 360° to 0°, unless you'd unwrap those values (see np.unwrap) such that values close to 0° would be reinterpreted as those same values +360°. But then you increase all contours again and you'd end up at contourlevels close to 2x360° and then yet another edge.

物理上与风向相关的数据集绝对不是您想要的,因为最终您将一直陷入无限循环,并为轮廓添加跳跃。那是因为轮廓实际上并不适合此类数据

For the nature of your dataset, which is physical and related to the wind direction, that is definitely not what you would want, because you would end up in an infinite loop of all the time adding that jump for the contours. That's because contours aren't really suited for this kind of data.

不,因此,有防风倒钩和颤动图,例如根据您的数据集:

No, for this reason, there are wind barbs and quiver plots, such as this one, based on your dataset:

生成该图片的代码很简单:

The code that generated that picture is simple:

x = np.load('arr.npy')
z = x/180*np.pi
u = np.cos(z)
v = np.sin(z)
plt.imshow(z, cmap='hot')
plt.quiver(u,v)

在我自己的研究中,我什至对箭头都不感兴趣自己,因为在我的工作范围内180°与0°相同,所以我只是画棍子,没有倒钩,没有头。

In my own research, I'm not even interested in the arrows themselves, because in my line of work 180° is the same as 0°, so I just draw sticks, without barbs, without heads.

我知道从技术上讲,这不是您想要的答案,但轮廓不适合此。如果您确实想要轮廓,可以将数据集按区域划分(例如 0< = angle< 20 等),然后绘制以这些区域中的每个区域仅带有半角的箭头(因此,每个域中,几个箭头都指向10°方向),但是通过这种方式,您将丢失定量数据。
一种替代方法是仅对每个上述域着色,并在其中添加文本标签以指示其值。这样,您将不会在360-0边界的边缘看到一系列紧密堆积的轮廓线。

I know it's technically not the answer you were hoping for, but contours are just not suited for this. If you'd really want "contours", you could split up the dataset in regions (e.g. 0 <= angle <20 and so on) and then either draw for each of those regions only arrows with the angle halfway (so per domain, several arrows all pointing in e.g. the 10° direction), but this way you'd loose quantitative data. An alternative would be to just color each of the aforementioned domains and add text labels inside them to indicate their value. That way, you wouldn't see a series of closely packed contourlines at the edge of the 360-0 boundary.

这篇关于使用matplotlib轮廓/轮廓处理循环数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆