diff --git a/docs/api.rst b/docs/api.rst index bf14a42eb..2423ca714 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -64,8 +64,10 @@ Possible ways for specifying the crs for plotting are: There is one layer-name that has a special meaning... the ``"all"`` layer. Any callbacks, features or plots added to this layer will be **executed on all other layers** as well! - You can always add features and callbacks to the ``all`` layer via the shortcut ``m.all. ...`` - + You can add features and callbacks to the ``all`` layer via: + - using the shortcut ``m.all. ...`` + - creating a dedicated ``Maps`` object via ``m_all = Maps(layer="all")`` or ``m_all = m.new_layer("all")`` + - using the "layer" kwarg of functions e.g. ``m.plot_map(layer="all")`` .. currentmodule:: eomaps diff --git a/eomaps/_containers.py b/eomaps/_containers.py index 44a26c85b..86848db71 100644 --- a/eomaps/_containers.py +++ b/eomaps/_containers.py @@ -123,9 +123,9 @@ def set_colorbar_position(self, pos=None, ratio=None, cb=None): """ if cb is None: - _, ax_cb, ax_cb_plot, orientation, _ = self._m._colorbar + _, _, ax_cb, ax_cb_plot, orientation, _ = self._m._colorbar else: - _, ax_cb, ax_cb_plot, orientation, _ = cb + _, _, ax_cb, ax_cb_plot, orientation, _ = cb if orientation == "horizontal": pcb = ax_cb.get_position() diff --git a/eomaps/eomaps.py b/eomaps/eomaps.py index 04a4e58df..c66a742ee 100644 --- a/eomaps/eomaps.py +++ b/eomaps/eomaps.py @@ -2810,7 +2810,11 @@ def add_colorbar( """ Add a colorbar to an existing figure. - (NOTE: the preferred way is to use `plot_map(colorbar=True)` instead!) + The colorbar always represents the data of the associated Maps-object + that was assigned in the last call to `m.plot_map()`. + + By default, the colorbar will only be visible on the layer of the associated + Maps-object (you can override this by providing an explicit "layer"-name). To change the position of the colorbar, use: @@ -2853,6 +2857,11 @@ def add_colorbar( The padding between the colorbar and the parent axes (as fraction of the plot-height (if "horizontal") or plot-width (if "vertical") The default is (0.05, 0.1, 0.1, 0.05) + layer : int, str or None, optional + The layer to put the colorbar on. + To make the colorbar visible on all layers, use `layer="all"` + If None, the layer of the associated Maps-object is used. + The default is None. log : bool, optional Indicator if the y-axis of the plot should be logarithmic or not. The default is False @@ -3024,7 +3033,7 @@ def add_colorbar( ) # hide the colorbar if it is not added to the currently visible layer - if layer != self.BM._bg_layer: + if layer not in [self.BM._bg_layer, "all"]: ax_cb.set_visible(False) ax_cb_plot.set_visible(False) m.BM._hidden_axes.add(ax_cb) @@ -3038,7 +3047,7 @@ def add_colorbar( self.BM.add_bg_artist(self._ax_cb_plot, layer) # remember colorbar for later (so that we can update its position etc.) - self._colorbar = [cbgs, ax_cb, ax_cb_plot, orientation, cb] + self._colorbar = [layer, cbgs, ax_cb, ax_cb_plot, orientation, cb] return [cbgs, ax_cb, ax_cb_plot, orientation, cb] diff --git a/eomaps/helpers.py b/eomaps/helpers.py index e07c170b2..43b9e7f05 100644 --- a/eomaps/helpers.py +++ b/eomaps/helpers.py @@ -824,17 +824,17 @@ def bg_layer(self, val): # a general callable to be called on every layer change self._do_on_layer_change(layer=val) + # hide all colorbars that are not no the visible layer for m in [self._m.parent, *self._m.parent._children]: - if m.layer != val: - if hasattr(m.figure, "ax_cb") and m.figure.ax_cb is not None: - m.figure.ax_cb.set_visible(False) - if hasattr(m.figure, "ax_cb_plot") and m.figure.ax_cb_plot is not None: - m.figure.ax_cb_plot.set_visible(False) - else: - if hasattr(m.figure, "ax_cb") and m.figure.ax_cb is not None: - m.figure.ax_cb.set_visible(True) - if hasattr(m.figure, "ax_cb_plot") and m.figure.ax_cb_plot is not None: - m.figure.ax_cb_plot.set_visible(True) + if getattr(m, "_colorbar", None) is not None: + [layer, cbgs, ax_cb, ax_cb_plot, orientation, cb] = m._colorbar + + if layer != val: + ax_cb.set_visible(False) + ax_cb_plot.set_visible(False) + else: + ax_cb.set_visible(True) + ax_cb_plot.set_visible(True) # self.canvas.flush_events() self._clear_temp_artists("on_layer_change")