import matplotlib.pyplot as plt from postprocessor import * class InteractiveGUI: """ This is a matplotlib interactive session to restrain analysis to a specific area """ def onbuttonrelease(self, event): """Deal with click events""" button = ["left", "middle", "right"] toolbar = plt.get_current_fig_manager().toolbar if toolbar.mode == "zoom rect" and event.inaxes == self.ax_col: print("zooming ") xlim = self.ax_col.get_xlim() ylim = self.ax_col.get_ylim() self.reset_mask() elif self.add_mask and event.inaxes == self.ax_col: self.plot_side() plt.draw() def onbuttonpress(self, event): """Deal with click events""" button = ["left", "middle", "right"] toolbar = plt.get_current_fig_manager().toolbar if toolbar.mode != "": print( "You clicked on something, but toolbar is in mode {:s}.".format( toolbar.mode ) ) print(self.add_mask) if self.add_mask and toolbar.mode == "" and event.inaxes == self.ax_col: ix, iy = event.xdata, event.ydata print("Add patch {}, {}".format(ix, iy)) xlim = self.ax_col.get_xlim() ylim = self.ax_col.get_ylim() radius = 0.05 * min(abs(xlim[1] - xlim[0]), abs(ylim[1] - ylim[0])) circle = mpatches.Circle( [ix, iy], radius, color="black", alpha=0.1, ec="none" ) self.circles.append(circle) self.ax_col.add_artist(circle) self.ax_col.draw_artist(circle) self.patch_mask = self.patch_mask | ( (self.xx - ix) ** 2 + (self.yy - iy) ** 2 < radius ** 2 ) # self.plot_side() def onkeypress(self, event): """whenever a key is pressed""" if not event.inaxes: return if event.key == "t": self.add_mask = not self.add_mask print("Add mode is {}".format(self.add_mask)) elif event.key == "r": self.reset_mask() def plot_side(self): if self.add_mask: mask = (self.patch_mask & self.mask).flatten() else: mask = self.mask.flatten() self.ax_gamma.clear() plt.sca(self.ax_gamma) plot_dcsdrho(self.fluct_maps, mask, tag=self.tag) self.ax_pdf.clear() plt.sca(self.ax_pdf) sigma_pdf(self.fluct_maps, mask, tag=self.tag, nb_bin_hist=self.args.pdf_nb_bin) def reset_mask(self): xlim = self.ax_col.get_xlim() ylim = self.ax_col.get_ylim() self.mask = ( (self.xx >= xlim[0]) & (self.xx <= xlim[1]) & (self.yy >= ylim[0]) & (self.yy <= ylim[1]) ) self.patch_mask = np.full(self.mask.shape, False) for circle in self.circles: circle.remove() self.circles = [] self.plot_side() plt.draw() def __init__(self, num, path="./", pp_params=None): """ Interactive plotting """ pp = PostProcessor(path, num, pp_params=pp_params, tag="interactive") pp.pdf_coldens("z") fluct_map = pp.get_value("/maps/avg_map_coldens_z") rr = pp.get_value("/maps/rr_z") fig, ax = plt.subplots(2) im = ax[0].imshow(fluct_map, origin="lower", cmap="RdBu_r") cbar = plt.colorbar() fig.canvas.mpl_connect("button_release_event", self.onbuttonrelease) fig.canvas.mpl_connect("button_press_event", self.onbuttonpress) fig.canvas.mpl_connect("key_press_event", self.onkeypress) plt.tight_layout() plt.show()