简体   繁体   English

在底图中使用 pcolormesh 填充区域

[英]Hatch area using pcolormesh in Basemap

I try to hatch only the regions where I have statistically significant results.我尝试只孵化我有统计显着结果的区域。 How can I do this using Basemap and pcolormesh?如何使用 Basemap 和 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='/')

I have a simple solution for this problem, using only pcolormesh and not pcolor : Plot the color mesh, then hatch the entire plot, and then plot the original mesh again, this time by masking statistically significant cells, so that the only hatching visible is those on significant cells.对于这个问题,我有一个简单的解决方案,只使用pcolormesh而不是pcolor :绘制颜色网格,然后绘制整个图,然后再次绘制原始网格,这次通过屏蔽统计显着的单元格,以便唯一可见的阴影是那些在重要细胞上。 Alternatively, you can put a marker on every cell (looks good too), instead of hatching the entire figure.或者,您可以在每个单元格上放置一个标记(看起来也不错),而不是填充整个图形。

(I use cartopy instead of basemap , but this shouldn't matter.) (我使用cartopy而不是basemap ,但这应该无关紧要。)

Step 1: Plot your field ( z ) normally, using pcolormesh .第 1 步:使用pcolormesh正常绘制您的字段 ( z )。

mesh = plt.pcolormesh(x,y,z)

where x/y can be lons/lats.其中 x/y 可以是 lons/lats。

Step 2: Hatch the entire plot.第 2 步:填充整个图。 For this, use fill_between :为此,请使用fill_between

hatch = plt.fill_between([xmin,xmax],y1,y2,hatch='///////',color="none",edgecolor='black')

Check details of fill_between to set xmin , xmax , y1 and y2 .检查fill_between详细信息以设置xminxmaxy1y2 You simply define two horizontal lines beyond the bounds of your plot, and hatch the area in between.您只需定义超出绘图边界的两条水平线,并在其间填充区域。 Use more, or less / s to set hatch density.使用更多或更少/ s 来设置填充密度。

To adjust hatch thickness, use below lines:要调整剖面线厚度,请使用以下几行:

import matplotlib as mpl
mpl.rcParams['hatch.linewidth'] = 0.3

As an alternative to hatching everything, you can plot all your xy points (or, lon-lat couples) as markers.作为孵化所有内容的替代方法,您可以将所有 xy 点(或经纬度点对)绘制为标记。 A simple solution is putting a dot (x also looks good).一个简单的解决方案是放置一个点(x 看起来也不错)。

hatch = plt.plot(x,y,'.',color='black',markersize=1.5)

One of the above will be the basis of your 'hatch'.以上之一将是您的“孵化”的基础。 This is how it should look after Step 2:这是它在第 2 步之后的样子:

这是它在第 2 步之后的样子

Step 3: On top of these two, plot your color mesh once again with pcolormesh , this time masking cells containing statistically significant values.第 3 步:在这两个之上,再次使用pcolormesh绘制您的颜色网格,这次屏蔽包含统计显着值的单元格。 This way, the markers on your 'insignificant' cells become invisible again, while significant markers stay visible.这样,“无关紧要”单元格上的标记将再次变得不可见,而重要标记仍然可见。

Assuming you have an identically sized array containing the t statistic for each cell ( t_z ), you can mask significant values using numpy 's ma module.假设您有一个大小相同的数组,其中包含每个单元格 ( t_z ) 的t 统计量,您可以使用numpyma模块屏蔽重要值。

z_masked = numpy.ma.masked_where(t_z >= your_threshold, z)

Then, plot the color mesh, using the masked array.然后,使用掩码数组绘制颜色网格。

mesh_masked = plt.pcolormesh(x,y,z_masked)

Use zorder to make sure the layers are in correct order.使用zorder确保图层顺序正确。 This is how it should look after Step 3:这是它在第 3 步之后的样子:

这是它在第 3 步之后的样子

It seems pcolormesh does not support hatching (see https://github.com/matplotlib/matplotlib/issues/3058 ).似乎pcolormesh不支持孵化(参见https://github.com/matplotlib/matplotlib/issues/3058 )。 Instead, the advice is to use pcolor , which starting from this example would look like,相反,建议使用pcolor ,从 这个例子开始看起来像,

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()

where a mask array is used to get the values of z greater than 0.3 and these are hatched using pcolor .其中掩码数组用于获取大于 0.3 的 z 值,并使用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.为了避免在顶部绘制另一种颜色(这样你只能得到阴影),我将 alpha 设置为 0。在pcolor ,这感觉有点像黑客。 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 .请参阅此示例Python:从 matplotlib 热图及其图例中保留 Numpy NaN 值 This may be more tricky for basemaps, etc than just choosing areas with pcolor .对于底图等而言,这可能比仅使用pcolor选择区域更棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM