def on_button_release(event): if event.button != 3: # Right-click. return images = [*_iter_overlapping_normed_images(artist, event)] if not images: return def edit_norm(_arg=None): # Only some backends pass in an argument. axs = plt.figure().subplots(len(images), 1, squeeze=False)[:, 0] for ax, image in zip(axs, images): array = image.get_array() ax.hist(array.ravel(), _hist_bins(image), histtype="stepfilled") if isinstance(image.norm, mpl.colors.LogNorm): ax.set(xscale="log") elif array.dtype.kind in "iu": # not log. ax.xaxis.get_major_locator().set_params(integer=True) def on_select(vmin, vmax, *, _image=image): array = _image.get_array() if vmin == vmax: _image.set_clim((array.min(), array.max())) else: _image.set_clim((vmin, vmax)) _image.figure.canvas.draw() if hasattr(SpanSelector, "extents"): ss = SpanSelector(ax, on_select, "horizontal", useblit=True, interactive=True) ss.extents = (image.norm.vmin, image.norm.vmax) else: ss = SpanSelector(ax, on_select, "horizontal", useblit=True, span_stays=True) ss.stay_rect.set(x=image.norm.vmin, width=image.norm.vmax - image.norm.vmin, visible=True) _selectors.setdefault(ax, []).append(ss) plt.show(block=False) gui_event = event.guiEvent pkg = type(gui_event).__module__.split(".")[0] if pkg == "gi": from gi.repository import Gtk menu = Gtk.Menu() item = Gtk.MenuItem.new_with_label("Norm") menu.append(item) item.connect("activate", edit_norm) item.show() menu.popup_at_pointer(gui_event) elif pkg.startswith(("PyQt", "PySide")): from matplotlib.backends.qt_compat import QtWidgets menu = QtWidgets.QMenu() menu.addAction("Norm", edit_norm) menu.exec(gui_event.globalPos()) elif pkg == "tkinter": from tkinter import Menu menu = Menu(gui_event.widget, tearoff=0) menu.add_command(label="Norm", command=edit_norm) try: menu.tk_popup(gui_event.x_root, gui_event.y_root) finally: menu.grab_release() elif pkg == "wx": import wx menu = wx.Menu() item = menu.Append(wx.ID_ANY, "Norm") gui_event.EventObject.Bind(wx.EVT_MENU, edit_norm, id=item.Id) gui_event.EventObject.PopupMenu(menu) else: raise NotImplementedError("The current backend is not supported")
def on_button_release(event): if event.button != 3: # Right-click. return images = [*_iter_overlapping_normed_images(artist, event)] if not images: return def edit_norm(_arg=None): # Only some backends pass in an argument. try: im, = images except ValueError: raise NotImplementedError from None array = im.get_array().ravel() sub_ax = plt.figure().subplots() sub_ax.hist(array, _hist_bins(im), histtype="stepfilled") if isinstance(im.norm, mpl.colors.LogNorm): sub_ax.set(xscale="log") def on_select(vmin, vmax): if vmin == vmax: im.set_clim((array.min(), array.max())) else: im.set_clim((vmin, vmax)) im.figure.canvas.draw() ss = sub_ax.__ss = SpanSelector(sub_ax, on_select, "horizontal", useblit=True, span_stays=True) ss.stay_rect.set(x=im.norm.vmin, width=im.norm.vmax - im.norm.vmin, visible=True) plt.show(block=False) gui_event = event.guiEvent pkg = type(gui_event).__module__.split(".")[0] if pkg == "gi": from gi.repository import Gtk menu = Gtk.Menu() item = Gtk.MenuItem.new_with_label("Norm") menu.append(item) item.connect("activate", edit_norm) item.show() menu.popup_at_pointer(gui_event) elif pkg.startswith(("PyQt", "PySide")): from matplotlib.backends.qt_compat import QtWidgets menu = QtWidgets.QMenu() menu.addAction("Norm", edit_norm) menu.exec(gui_event.globalPos()) elif pkg == "tkinter": from tkinter import Menu menu = Menu(gui_event.widget, tearoff=0) menu.add_command(label="Norm", command=edit_norm) try: menu.tk_popup(gui_event.x_root, gui_event.y_root) finally: menu.grab_release() elif pkg == "wx": import wx menu = wx.Menu() item = menu.Append(wx.ID_ANY, "Norm") gui_event.EventObject.Bind(wx.EVT_MENU, edit_norm, id=item.Id) gui_event.EventObject.PopupMenu(menu) else: raise NotImplementedError("The current backend is not supported")