Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Open
Description
Problem
I would like a SpanSelector that obscures everywhere but the area selected. This is nice for things like selecting a region of a histogram to threshold an image.
Example of what I want:
Code for below
importmatplotlib.pyplotaspltfrommatplotlib.patchesimportRectanglefrommatplotlib.widgetsimportSpanSelectorimportnumpyasnpdefsee_through_span_selector(ax,onselect=None,init_extents=None):""" A spanselector that is see through in the selected range. Parameters ---------- ax : matplotlib.axes.Axis onselect : callback init_extents : (float, float) If *None* then use the 25% and 75% of xlim """fig=ax.get_figure()props=dict(alpha=.5,facecolor="k")trans=ax.get_xaxis_transform()w,h=0,1left_rect=Rectangle((0,0),w,h,transform=trans,visible=True,**props)ax.add_patch(left_rect)right_rect=Rectangle((0,0),w,h,transform=trans,visible=True,**props)ax.add_patch(right_rect)defonmove(xmin,xmax):xlims=ax.get_xlim()left_rect.set_width(xmin-min(xlims))left_rect.set_x(min(xlims))right_rect.set_x(xmax)right_rect.set_width(max(xlims)-xmax)onselect(xmin,xmax)fig.canvas.draw_idle()defnull(*args,**kwargs):passspan=SpanSelector(ax,null,"horizontal",props=dict(alpha=0,facecolor="k"),interactive=True,drag_from_anywhere=True,onmove_callback=onmove,useblit=True )ifinit_extentsisNone:init_extents= (xlims[0]*.25,xlims[1]*.75)span.extents=init_extentsonmove(*span.extents)returnspannp.random.seed(19680801)N=128img=np.random.randn(N,N)fig,axs=plt.subplots(1,2,figsize=(10,5))im=axs[0].imshow(img)axs[1].hist(img.flatten(),bins='auto')defupdate(vmin,vmax):im.norm.vmin=vminim.norm.vmax=vmaxspan=see_through_span_selector(axs[1],update,init_extents= (-2,2))
Proposed solution
Add an option to spanselector that effectively does the above.