基于网格掩盖的轮廓图 [英] Contour plot masked on the basis of grid

查看:79
本文介绍了基于网格掩盖的轮廓图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于 (x,y) 坐标表面生成一个等高线图,其中包含一个立方体,其中包含任何数据 z .以下是它的散点图.

I am trying to generate a contour plot based on (x,y) coordinates surface with a cube in it that dosent include any data z . Following is its scatterplot.

我使用以下代码生成网格并插入数据以绘制这样的等高线图.我尝试掩盖插值数据 Zi ,但它仍然为我提供了未掩盖的轮廓图.我也尝试掩盖x和y坐标,但是这没什么用.

I use the following code to generate a mesh and interpolate data to plot such a contour map. I try to mask the interpolated data Zi but it still gives me an unmasked contour plot. I also tried to mask x and y coordinates but that dosent do any good.

x = centre_unadj['X [mm]']
y = centre_unadj['Y [mm]']
z = centre_unadj['LDA1-RMS [m/s]']

plt.figure(num=None, figsize=(20, 15), dpi=80, facecolor='w', edgecolor='k')
xi,yi = np.meshgrid(x,y)
mask =(yi> 0) & (yi< 25) & (xi > -53) & (xi < -25) 
#mask_xi = (xi > -53) & (xi < -25) 
#mask_yi = (yi> 0) & (yi< 25)
#yi = ma.masked_array(yi,mask =(yi> 0) & (yi< 25) )
#xi = ma.masked_array(xi,mask=((xi > -53) & (xi < -25) ))
zi = scipy.interpolate.griddata((x,y), z, (xi, yi) , method='cubic')
zi = ma.masked_array(zi, mask = ((yi> 0) & (yi< 25) & (xi > -53) & (xi < -25)) )

#zi[mask]=np.nan

plt.contourf( xi,yi,zi,100)

plt.colorbar()
plt.show()

这是运行上面的代码后得到的图.

This is the plot I get after running the above code.

我只是不想在没有数据点的立方区域内进行任何轮廓插值.

I just dont want any contour interpolation inside the cubic area where there are no datapoints.

推荐答案

问题出在meshgrid生成上.y 中的值从 0 到 40 变化了十几次.因此,生成的 xi yi 将是真正不直观的矩阵.

The problem is in the meshgrid generation. The values in y go from 0 to 40 more than a dozen times. Thus, the generated xi and yi will be really unintuitive matrixes.

生成meshgrid的正确方法如下:

The proper way of generating the meshgrid is the following:

xi,yi = np.meshgrid(np.linspace(x.min(),x.max(),200),np.linspace(y.min(),y.max(),200))

示例

我生成了一些形状相似的数据:

Example

I have generated some data with a similar shape:

import scipy.signal as sgn
import scipy.interpolate as intr
import numpy.ma as ma
x = np.linspace(-100,0,500)
y = sgn.sawtooth(2 * np.pi * .2 * x)
mask = (x>-50) & (x<-25)
y[mask] = (sgn.sawtooth(2 * np.pi * .2 * x[mask])+1)/2
y = (y+1)*25
plt.plot(x,y)
z = np.sin(2*np.pi*.1*x)+np.sin(2*np.pi*.1*y)

x与y的关系图看起来像这样:

Such that the plot x vs y looks like:

您实际使用的代码生成以下图:

The code you are actually using generates the following plot:

xi,yi = np.meshgrid(x,y)
mask =(yi> 0) & (yi< 25) & (xi > -53) & (xi < -25) 
zi = intr.griddata((x,y), z, (xi, yi) , method='cubic')
zi = ma.masked_array(zi, mask = mask )
plt.contourf( xi,yi,zi,100); plt.colorbar()

获得网格数据的插值产生了意外和不正确的结果,导致获得的contourf.实际上,绘制 plt.imshow(mask)会显示矩阵中平方(y> 0)&的值.(y< 25)&(x> -53)&(x< -25)放置在矩阵中.

The interpolation to obtain the grid data yields unexpected and incorrect results, which result in the obtained contourf. In fact, plotting plt.imshow(mask) reveals the positions in the matrix where the values inside the square (y > 0) & (y < 25) & (x > -53) & (x < -25) are placed in the matrix.

当网格被定义为建议的时候,结果是这样的:

When the meshgrid is defined as proposed, the result is this one instead:

这篇关于基于网格掩盖的轮廓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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