I have interpolated a plot and extended the x and y which gave me the following contour plot. How can I rearrange the Z matrix so that the points outside the red box are 0.
x=data.columns.astype('float64')
y=data.index.to_numpy()
z=data.to_numpy()
X,Y = np.meshgrid(x, y)
ratio = 4
x_new = x*ratio
y_new=y*ratio
#plt.plot(x_new,y_new)
f = interpolate.interp2d(x, y, z, kind='linear')
z_new = f(x_new, y_new)
X_new,Y_new = np.meshgrid(x_new, y_new)
plt.contourf(X_new, Y_new, znew, alpha = 1, cmap=plt.cm.inferno)
CodePudding user response:
Here is a minimal example, which should be easy to adapt to your case.
The np.where
function is your friend here:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-15, 15, 301)
y = np.linspace(-15, 5, 201)
X_new, Y_new = np.meshgrid(x, y)
znew = np.random.randn(*X_new.shape)
znew = np.where((X_new > 5) & (X_new < 10) &
(Y_new > -2) & (Y_new < 2), znew, 0)
plt.contourf(X_new, Y_new, znew, alpha = 1, cmap=plt.cm.inferno)