class MplPolygonalROI(AbstractMplRoi): """ Matplotlib ROI for polygon selections Parameters ---------- axes : `~matplotlib.axes.Axes` The Matplotlib axes to draw to. roi : `~glue.core.roi.Roi`, optional If specified, this ROI will be used and updated, otherwise a new one will be created. """ _roi_cls = PolygonalROI def __init__(self, axes, roi=None): super(MplPolygonalROI, self).__init__(axes, roi=roi) self.plot_opts = {'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR, 'alpha': 0.3} self._patch = Polygon(np.array(list(zip([0, 1], [0, 1]))), zorder=100) self._patch.set_visible(False) self._axes.add_patch(self._patch) def _sync_patch(self): if self._roi.defined(): x, y = self._roi.to_polygon() self._patch.set_xy(list(zip(x + [x[0]], y + [y[0]]))) self._patch.set_visible(True) self._patch.set(**self.plot_opts) else: self._patch.set_visible(False) def start_selection(self, event, scrubbing=False): if event.inaxes != self._axes: return False if scrubbing or event.key == SCRUBBING_KEY: if not self._roi.defined(): return False elif not self._roi.contains(event.xdata, event.ydata): return False self._store_previous_roi() self._store_background() if scrubbing or event.key == SCRUBBING_KEY: self._scrubbing = True self._cx = event.xdata self._cy = event.ydata else: self.reset() self._roi.add_point(event.xdata, event.ydata) self._mid_selection = True self._sync_patch() self._draw() def update_selection(self, event): if not self._mid_selection or event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False if self._scrubbing: self._roi.move_to(event.xdata - self._cx, event.ydata - self._cy) self._cx = event.xdata self._cy = event.ydata else: self._roi.add_point(event.xdata, event.ydata) self._sync_patch() self._draw() def finalize_selection(self, event): self._scrubbing = False self._mid_selection = False self._patch.set_visible(False) self._draw()
class MplPolygonalROI(AbstractMplRoi): """ Defines and displays polygonal ROIs on matplotlib plots Attributes: plot_opts: Dictionary instance A dictionary of plot keywords that are passed to the patch representing the ROI. These control the visual properties of the ROI """ def __init__(self, axes, roi=None): """ :param axes: A matplotlib Axes object to attach the graphical ROI to """ AbstractMplRoi.__init__(self, axes, roi=roi) self.plot_opts = { 'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR, 'alpha': 0.3 } self._setup_patch() def _setup_patch(self): self._patch = Polygon(np.array(list(zip([0, 1], [0, 1])))) self._patch.set_zorder(100) self._patch.set(**self.plot_opts) self._axes.add_patch(self._patch) self._patch.set_visible(False) self._sync_patch() def _roi_factory(self): return PolygonalROI() def _sync_patch(self): # Update geometry if not self._roi.defined(): self._patch.set_visible(False) else: x, y = self._roi.to_polygon() self._patch.set_xy(list(zip(x + [x[0]], y + [y[0]]))) self._patch.set_visible(True) # Update appearance self._patch.set(**self.plot_opts) # Refresh self._axes.figure.canvas.draw() def start_selection(self, event, scrubbing=False): if event.inaxes != self._axes: return False if scrubbing or event.key == SCRUBBING_KEY: if not self._roi.defined(): return False elif not self._roi.contains(event.xdata, event.ydata): return False self._roi_store() if scrubbing or event.key == SCRUBBING_KEY: self._scrubbing = True self._cx = event.xdata self._cy = event.ydata else: self.reset() self._roi.add_point(event.xdata, event.ydata) self._mid_selection = True self._sync_patch() def update_selection(self, event): if not self._mid_selection or event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False if self._scrubbing: self._roi.move_to(event.xdata - self._cx, event.ydata - self._cy) self._cx = event.xdata self._cy = event.ydata else: self._roi.add_point(event.xdata, event.ydata) self._sync_patch() def finalize_selection(self, event): self._scrubbing = False self._mid_selection = False self._patch.set_visible(False) self._axes.figure.canvas.draw()
# draw everything xs = np.linspace(xknots[0],xknots[-1],200) from scipy.integrate import quad ymodel = nMC*Model(xs)/quad(Model,0,2)[0] yprior = np.vectorize(lambda x: unfolded(refCoefs,x))(xs) yfitted = np.vectorize(lambda x: folded (coefs,x))(xs[xs<1.8]) yunfolded = np.vectorize(lambda x: unfolded(coefs,x))(xs) plt.figure(1) from matplotlib.patches import Polygon poly = Polygon(MakeSigmaPatch(unfolded, coefs, cov, xknots), closed=True) poly.set(ec="b",fc="b",alpha=0.1,fill=True,zorder=0) plt.gca().add_patch(poly) plt.plot(xs,ymodel,"g--",lw=2,label="$t(x)$",zorder=1) plt.plot(xs,yprior,"y",lw=3,label="$g(x)$",zorder=1) plt.plot(xs[xs<1.8],yfitted,"r",lw=1,label="$f(y)$",zorder=3) plt.plot(xs,yunfolded,"b",lw=2,label="$b(x)$",zorder=2) xh, w, werr = GetScaledHistogram(data,bins=20,range=(folded.GetStart(),folded.GetStop())) plt.errorbar(xh,w,werr,fmt="ko",capsize=0,label="data",zorder=4) plt.ylim(ymin=0) plt.xlabel("x")
class MplPolygonalROI(AbstractMplRoi): """ Defines and displays polygonal ROIs on matplotlib plots Attributes: plot_opts: Dictionary instance A dictionary of plot keywords that are passed to the patch representing the ROI. These control the visual properties of the ROI """ def __init__(self, axes): """ :param axes: A matplotlib Axes object to attach the graphical ROI to """ AbstractMplRoi.__init__(self, axes) self.plot_opts = {'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR, 'alpha': 0.3} self._setup_patch() def _setup_patch(self): self._patch = Polygon(np.array(list(zip([0, 1], [0, 1])))) self._patch.set_zorder(100) self._patch.set(**self.plot_opts) self._axes.add_patch(self._patch) self._patch.set_visible(False) self._sync_patch() def _roi_factory(self): return PolygonalROI() def _sync_patch(self): # Update geometry if not self._roi.defined(): self._patch.set_visible(False) else: x, y = self._roi.to_polygon() self._patch.set_xy(list(zip(x + [x[0]], y + [y[0]]))) self._patch.set_visible(True) # Update appearance self._patch.set(**self.plot_opts) # Refresh self._axes.figure.canvas.draw() def start_selection(self, event): if event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False elif not self._roi.contains(event.xdata, event.ydata): return False self._roi_store() if event.key == SCRUBBING_KEY: self._scrubbing = True self._cx = event.xdata self._cy = event.ydata else: self.reset() self._roi.add_point(event.xdata, event.ydata) self._mid_selection = True self._sync_patch() def update_selection(self, event): if not self._mid_selection or event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False if self._scrubbing: self._roi.move_to(event.xdata - self._cx, event.ydata - self._cy) self._cx = event.xdata self._cy = event.ydata else: self._roi.add_point(event.xdata, event.ydata) self._sync_patch() def finalize_selection(self, event): self._scrubbing = False self._mid_selection = False self._patch.set_visible(False) self._axes.figure.canvas.draw()