Skip to content

Commit

Permalink
allow adding colorbars to the "all" layer as well
Browse files Browse the repository at this point in the history
add docstring for "layer" on m.add_colorbar
  • Loading branch information
raphaelquast committed Apr 5, 2022
1 parent 2f1cbbb commit 9d13222
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
6 changes: 4 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions eomaps/_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 12 additions & 3 deletions eomaps/eomaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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]

Expand Down
20 changes: 10 additions & 10 deletions eomaps/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 9d13222

Please sign in to comment.