简体   繁体   中英

Generate a triangular mask for a contour plot given three vertices

Here is some random data that I generate :

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)

y = (sgn.sawtooth(2 * np.pi * .2 * x)+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)

This gives me this figure : 在此输入图像描述

Then I build a contour plot :

xi,yi = np.meshgrid(np.linspace(x.min(),x.max(),200),np.linspace(y.min(),y.max(),200))   
zi = intr.griddata((x,y), z, (xi, yi) , method='cubic')    
plt.contourf( xi,yi,zi,100); plt.colorbar()

在此输入图像描述 s

For a Square mask , I do this :

xi,yi = np.meshgrid(np.linspace(x.min(),x.max(),200),np.linspace(y.min(),y.max(),200))
mask =(yi> 25) & (yi< 35) & (xi > -55) & (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() 

在此输入图像描述

My question is , how do I go about putting a triangular mask at the same base location as that of a square given three coordinates of the triangle ie (-50, 0) , (-25,0) ,( -37 , 25) .

Try something along these lines:

mask =(yi> 25) & (yi< 35) & (xi > -55) & (xi < -25) & ((xi+95) > 2*yi)

in other words add ((xi+95) > 2*yi) to your mask. You might have to adjust some of the constants to get exactly what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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