Skip to content

Commit

Permalink
Add function to set the colorbar limits
Browse files Browse the repository at this point in the history
The function `set_colorbar_limits` allows the user to set the limits of
the colorbar without affecting the limits of the normalization.

This commit also fixes atmtools#314 (`center_colorbar`).
  • Loading branch information
lkluft authored and Simon Pfreundschuh committed Sep 28, 2020
1 parent 4276ef5 commit d332b6a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/typhon.plots.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ plots
profile_z
ScalingFormatter
scatter_density_plot_matrix
set_colorbar_limits
set_xaxis_formatter
set_yaxis_formatter
sorted_legend_handles_labels
Expand Down
45 changes: 41 additions & 4 deletions typhon/plots/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
logger = logging.getLogger(__name__)

__all__ = [
'set_colorbar_limits',
'center_colorbar',
'figsize',
'styles',
Expand All @@ -29,8 +30,36 @@
]


def center_colorbar(cb):
"""Center a diverging colorbar around zero.
def set_colorbar_limits(cb, vmin=-1, vmax=1):
"""Set colorbar limits.
Note:
The normalization of the :class:`~matplotlib.cm.ScalarMappable`
is not affected.
Parameters:
cb (matplotlib.colorbar.Colorbar): Colorbar to center.
vmin (float): Lower limit of the colorbar axis.
vmax (float): Upper limit of the colorbar axis.
See also:
:func:`~typhon.plots.center_colorbar`
Center a diverging colorbar.
"""
cb.ax.set_ylim(vmin, vmax)
cb.outline.set_transform(cb.ax.transAxes)
cb.outline.set_xy(
np.array([
[0, 0],
[0, 1],
[1, 1],
[1, 0],
]),
)


def center_colorbar(cb, center=0.0):
"""Center a diverging colorbar.
Convenience function to adjust the color limits of a colorbar. The function
multiplies the absolute maximum of the data range by ``(-1, 1)`` and uses
Expand All @@ -42,6 +71,11 @@ def center_colorbar(cb):
Parameters:
cb (matplotlib.colorbar.Colorbar): Colorbar to center.
center (float): Fix point to center the colorbar around.
See also:
:func:`~typhon.plots.set_colorbar_limits`
Set colorbar limits.
Examples:
Expand All @@ -60,8 +94,11 @@ def center_colorbar(cb):
plt.show()
"""
# Set color limits to +- the absolute maximum of the data range.
cb.set_clim(np.multiply((-1, 1), np.max(np.abs(cb.get_clim()))))
vmin, vmax = cb.mappable.get_clim()
absmax = np.max(np.abs([vmin, vmax]))
cb.mappable.set_clim(-absmax + center, absmax + center)

set_colorbar_limits(cb, vmin + center, vmax + center)


def figsize(w, portrait=False):
Expand Down

0 comments on commit d332b6a

Please sign in to comment.