Reputation: 79
im wondering if anyone have any idea how to mask or hide a specific area in a contour plot in python, here is a part of my code
self.fig=plt.figure()
delta = 0.025
xmin=4
xmin=6
x=np.arange(4,6,delta)
ymin=85
ymax=91
y = np.arange(85, 91, delta)
X, Y = np.meshgrid(x, y)
Z=formel()//here im using a specific formula to calculate Z
plt.gcf().subplots_adjust(left=0.16)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
self.CS = plt.contour(X, Y, Z)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.plot(pointX, pointY, 'kx')
plt.clabel(self.CS, inline=1, fontsize=10)
self.canvas = FigureCanvasTkAgg(self.fig, self)
self.canvas.get_tk_widget().config(width=400,height=400)`
here is how I want to mask the areas
thank you guys
Upvotes: 2
Views: 409
Reputation: 46779
You could use PathPatch
to display your mask over the top as follows:
import numpy as np
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import matplotlib.path as mpath
ax = plt.gca()
x = np.linspace(0.04, 0.06, 100)
y = np.random.random(100) * [0.91 - 0.85] + 0.85
top, right, bottom, left = y.max(), x.max(), y.min(), x.min()
mask_height = (top - bottom) * 0.4 # 40% mask coverage
plt.plot(x, y, zorder=1)
ax.set_xlim(left, right)
ax.set_ylim(bottom, top)
path_data = [
(mpath.Path.MOVETO, (left, bottom)),
(mpath.Path.LINETO, (right, bottom)),
(mpath.Path.LINETO, (left, mask_height + bottom)),
(mpath.Path.CLOSEPOLY, (0,0)),
(mpath.Path.MOVETO, (right , top)),
(mpath.Path.LINETO, (right, top - mask_height)),
(mpath.Path.LINETO, (left, top)),
(mpath.Path.CLOSEPOLY, (0, 0)),
]
codes, verts = zip(*path_data)
path = mpath.Path(verts, codes)
patch = mpatches.PathPatch(path, facecolor=(0.8, 0.8, 0.8), lw=2, ec=(0.8, 0.8, 0.8), zorder=2)#, alpha=0.5)
ax.add_patch(patch)
plt.show()
This would give you something like:
Upvotes: 1