Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

python - Contour plot masked on the basis of grid

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.

scatterplot

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.

colorbar plot

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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)

Such that the plot x vs y looks like:

sawtooth

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

bad contourf

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.

mask imshow

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...