Pcolormesh没有正确的坐标

时间:2018-07-11 07:13:14

标签: python matplotlib

pcolormesh仅绘制外部坐标,必需的图是

enter image description here

newPress=[22.640048521269733, 8.7880990280388946, 8.5228130097742358, 6.1368312788828003, -0.012232139892299099,
 -0.0085282865280444931, 1.4163311525005766, 0.62047309770660242, 14.472422590937441, 15.268280645731416,
 17.653997267541644, 24.760479124815305, 22.374762503005076, 22.640048521269733]

poly3[0]=(-15.394, -15.394, -14.394, -14.394, 8.784995481927707, 12.394, 12.394, 15.394, 15.394,
 12.394, 12.394, -14.394, -14.394, -15.394)

poly3[1]=(13.0625, -13.0625, -13.0625, -17.5625, -17.5625, -15.74980786686838,
 -13.0625, -13.0625, 13.0625, 13.0625, 17.562, 17.562, 13.0625, 13.0625)

numcols, numrows = 200, 200
xi = np.linspace(min(poly3[0]), max(poly3[0]), numcols)
yi = np.linspace(min(poly3[1]), max(poly3[1]), numrows)
xi, yi = np.meshgrid(xi, yi)
x, y, z = poly3[0], poly3[1], newPress
zi = griddata(x, y, z, xi, yi,interp='linear')

fig2 = plt.figure(figsize=(8, 3.5))
ax2 = fig2.add_subplot(111)
ax2.scatter(x,y)
m = plt.pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r')
plt.show()

1 个答案:

答案 0 :(得分:0)

scipy.interpolate.griddata documentation的这一部分来看,griddata似乎是插在数据点周围的凸包上的。

  

fill_value:浮点型,可选值,用于填写要求的值   输入点的凸包之外的所有点。如果不   提供,则默认值为nan。此选项对   “最近”方法。

这意味着您总是也在您提供给griddata的积分之外获取数据。为了不显示这些区域,可以将根据点设置为无效(np.nan)值。就您而言,这很简单。

from matplotlib import pyplot as plt
from scipy.interpolate import griddata
import numpy as np

newPress=np.asarray([22.640048521269733, 8.7880990280388946, 8.5228130097742358, 6.1368312788828003, -0.012232139892299099,
 -0.0085282865280444931, 1.4163311525005766, 0.62047309770660242, 14.472422590937441, 15.268280645731416,
 17.653997267541644, 24.760479124815305, 22.374762503005076, 22.640048521269733])

poly3 = np.asarray([
    (
        -15.394, -15.394, -14.394, -14.394, 8.784995481927707,
        12.394, 12.394, 15.394, 15.394, 12.394, 12.394, -14.394,
        -14.394, -15.394
    ),
    (
        13.0625, -13.0625, -13.0625, -17.5625, -17.5625, -15.74980786686838,
        -13.0625, -13.0625, 13.0625, 13.0625, 17.562, 17.562, 13.0625, 13.0625
    )
])

numcols, numrows = 200, 200
xi = np.linspace(min(poly3[0]), max(poly3[0]), numcols)
yi = np.linspace(min(poly3[1]), max(poly3[1]), numrows)
x, y, z = poly3[0], poly3[1], newPress
xi, yi = np.meshgrid(xi, yi)
zi = griddata(poly3.T,z.T,np.asarray([xi,yi]).T, method='linear').T

fig2 = plt.figure(figsize=(8, 3.5))
ax2 = fig2.add_subplot(111)

ax2.scatter(x,y)

##finding the corners:
ll,lr,ur,ul = zip(x[[2,6,9,12]],y[[2,6,9,12]])

##removing data:
zi[np.logical_and(xi<ll[0],yi<ll[1])] = np.nan
zi[np.logical_and(xi>lr[0],yi<lr[1])] = np.nan
zi[np.logical_and(xi>ur[0],yi>ur[1])] = np.nan
zi[np.logical_and(xi<ul[0],yi>ul[1])] = np.nan

m = ax2.pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r')
plt.show()

结果如下:

result of the above code

请注意,您的示例代码不可运行,需要进行一些调整。请下次下次问问题时,请确保您的代码是Minimal, Complete, and Verifiable example

编辑

对于任意形状的多边形,您可以使用我概述的here技术,在其中定义由多边形和绘图区域轮廓组成的路径,然后将其覆盖在pcolormesh绘图上,并使用白色。请注意,为了使其正常工作,必须沿多边形的轮廓正确排序(我在下图的左子图中指出了这一点)。在下面的示例中,边缘以数学上的正向顺序(逆时针)排序,而绘图区域的边缘以相反方式(顺时针)排序:

from matplotlib import pyplot as plt
from scipy.interpolate import griddata
import numpy as np
from matplotlib.patches import Path, PathPatch

##generate an example polygon:
n_edges = 10
x_max = 15
y_max = 15

##note the sorting of the theta values
thetas = 2*np.pi*np.sort(np.random.rand(n_edges))
radii  = 0.5*(np.random.rand(len(thetas))+1)

x = np.cos(thetas)*x_max*radii
y = np.sin(thetas)*y_max*radii

values = np.random.rand(len(thetas))

fig, axes = plt.subplots(ncols=2)

##visualisation
axes[0].quiver(
    x[:-1],y[:-1],x[1:]-x[:-1],y[1:]-y[:-1],
    scale_units='xy',angles='xy',scale=1,
    lw = 3
)
axes[0].scatter(x,y,c=values,zorder=10,cmap='viridis_r')

##interpolation:
numcols, numrows = 200, 200
xi = np.linspace(min(x), max(x), numcols)
yi = np.linspace(min(y), max(y), numrows)
z = values
poly3 = np.asarray([x,y])
xi, yi = np.meshgrid(xi, yi)
zi = griddata(poly3.T,z.T,np.asarray([xi,yi]).T, method='linear').T

axes[1].scatter(x,y, zorder=10)
m = axes[1].pcolormesh(xi, yi, zi, alpha=0.15, cmap='viridis_r',zorder=8)

##getting the limits of the map:
x0,x1 = axes[1].get_xlim()
y0,y1 = axes[1].get_ylim()
map_edges = np.array([[x0,y0],[x0,y1],[x1,y1],[x1,y0]])

##masking the outsides of the polygon
mask = [map_edges,poly3.T]
codes = [[Path.MOVETO] + [Path.LINETO for p in m[1:]] for m in mask]
codes = np.concatenate(codes)
verts = np.concatenate(mask)
path = Path(verts,codes)
patch = PathPatch(path,facecolor='white', lw=0,zorder=9)
axes[1].add_patch(patch)

plt.show()

结果看起来像这样:

result of the second patch of code