在底图中使用pcolormesh填充区域 [英] Hatch area using pcolormesh in Basemap

查看:349
本文介绍了在底图中使用pcolormesh填充区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试仅对具有统计显着性结果的区域进行填充.如何使用底图和pcolormesh做到这一点?

I try to hatch only the regions where I have statistically significant results. How can I do this using Basemap and pcolormesh?

plt.figure(figsize=(12,12))

lons = iris_cube.coord('longitude').points
lats = iris_cube.coord('latitude').points

m = Basemap(llcrnrlon=lons[0], llcrnrlat=lats[0], urcrnrlon=lons[-1], urcrnrlat=lats[-1], resolution='l')

lon, lat = np.meshgrid(lons, lats)

plt.subplot(111)

cs = m.pcolormesh(lon, lat, significant_data, cmap=cmap, norm=norm, hatch='/')

推荐答案

似乎pcolormesh不支持阴影(请参见 https://github.com/matplotlib/matplotlib/issues/3058 ).相反,建议使用pcolor,它从示例开始喜欢,

It seems pcolormesh does not support hatching (see https://github.com/matplotlib/matplotlib/issues/3058). Instead, the advice is to use pcolor, which starting from this example would look like,

import matplotlib.pyplot as plt
import numpy as np

dx, dy = 0.15, 0.05
y, x = np.mgrid[slice(-3, 3 + dy, dy),
                slice(-3, 3 + dx, dx)]
z = (1 - x / 2. + x ** 5 + y ** 3) * np.exp(-x ** 2 - y ** 2)
z = z[:-1, :-1]
zm = np.ma.masked_less(z, 0.3)

cm = plt.pcolormesh(x, y, z)
plt.pcolor(x, y, zm, hatch='/', alpha=0.)
plt.colorbar(cm)
plt.show()

其中,使用掩码数组获取z的值大于0.3,并使用pcolor对其进行阴影处理.

where a mask array is used to get the values of z greater than 0.3 and these are hatched using pcolor.

为避免在顶部绘制另一种颜色(因此只产生阴影),我在pcolor中将alpha设置为0.替代方法是使用修补程序并将其分配给所需的区域.参见以下示例 Python:从matplotlib热图及其图例.对于底图而言,这可能比仅使用pcolor选择区域更为棘手.

To avoid plotting another colour over the top (so you get only hatching) I've set alpha to 0. in pcolor which feels a bit like a hack. The alternative is to use patch and assign to the areas you want. See this example Python: Leave Numpy NaN values from matplotlib heatmap and its legend. This may be more tricky for basemaps, etc than just choosing areas with pcolor.

这篇关于在底图中使用pcolormesh填充区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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