Reputation: 645
The plot below is a matplotlib contourf figure, with matplotlib.pyplot Rectangles overlayed on top to make a grid.
What would be the best way to white out the contourf plot outside of the Rectangle borders?
Upvotes: 2
Views: 924
Reputation: 339300
You can use a pcolormesh on top of the contour plot such that it hides the area below it.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-120.,120.,13)
X,Y = np.meshgrid(x,x)
Z = X*np.sqrt(np.abs(Y))*np.exp(-(X**2+Y**2)/3000.)
Xf = X.flatten()
Yf = Y.flatten()
Zf = Z.flatten()
cond = Xf**2+Yf**2 < 105**2
Xf = Xf[cond]
Yf = Yf[cond]
Zf = Zf[cond]
cond2 = X**2+Y**2 < 105**2
mask = np.zeros_like(X)
mask = np.ma.masked_array(mask,mask=cond2)
fig, ax = plt.subplots()
ax.tricontourf(Xf,Yf,Zf, levels=np.linspace(Z.min(), Z.max(), 10))
dx=np.diff(X[0,:])[0]; dy=np.diff(Y[:,0])[0]
ax.pcolormesh(X,Y,mask, zorder=2, cmap="gray", vmin=-1)
ax.pcolormesh(X-dx,Y-dy,mask, zorder=2, cmap="gray", vmin=-1)
ax.pcolormesh(X,Y-dy,mask, zorder=2, cmap="gray", vmin=-1)
ax.pcolormesh(X-dx,Y,mask, zorder=2, cmap="gray", vmin=-1)
ax.scatter(Xf,Yf, c="k", s=5, zorder=3)
plt.show()
Upvotes: 1
Reputation: 970
This is kind of a hack, but I think you could draw filled white rectangles in the size of your black border rectangles in order to hide the contour plot at these positions.
Upvotes: 0