Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: Modernize Colorbar Tick Labelling example #26255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions galleries/examples/ticks/colorbar_tick_labelling_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,62 @@
Colorbar Tick Labelling
=======================

Produce custom labelling for a colorbar.

Contributed by Scott Sinclair
Vertical colorbars have ticks, tick labels, and labels visible on the *y* axis,
horizontal colorbars on the *x* axis. The ``ticks`` parameter can be used to
set the ticks and the ``format`` parameter can be used to format the tick labels
of the visible colorbar axes. For further adjustments, the ``yaxis`` or
``xaxis`` axes of the colorbar can be retrieved using its ``ax`` property.
"""

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn

from matplotlib import cm
import matplotlib.ticker as mticker

# Fixing random state for reproducibility
np.random.seed(19680801)
rng = np.random.default_rng(seed=19680801)

# %%
# Make plot with vertical (default) colorbar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final little nit that can also happen in follow up - how do you feel about making this and the horizontal note instead be subsection titles?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not make them be subsection titles as both vertical and horizontal colorbars work exactly the same way. Subsections would be appropriate if we had different cases to contrast, in my opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hear you, what I'm thinking of is that in the sidebar you can scan quickly to click to find your case. Also maybe that if what you want to highlight is that they're exactly the same, then I'd maybe do this as a side by side subplot so that I could stack the identical code next to each other...either way I'll merge this and push that off.


fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)
data = rng.standard_normal((250, 250))

cax = ax.imshow(data, cmap=cm.coolwarm)
cax = ax.imshow(data, vmin=-1, vmax=1, cmap='coolwarm')
ax.set_title('Gaussian noise with vertical colorbar')

# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar
cbar = fig.colorbar(cax,
ticks=[-1, 0, 1],
format=mticker.FixedFormatter(['< -1', '0', '> 1']),
extend='both'
)
labels = cbar.ax.get_yticklabels()
labels[0].set_verticalalignment('top')
labels[-1].set_verticalalignment('bottom')

# %%
# Make plot with horizontal colorbar

fig, ax = plt.subplots()

data = np.clip(randn(250, 250), -1, 1)
data = np.clip(data, -1, 1)

cax = ax.imshow(data, cmap=cm.afmhot)
cax = ax.imshow(data, cmap='afmhot')
ax.set_title('Gaussian noise with horizontal colorbar')

cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal')
cbar.ax.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar
# Add colorbar and adjust ticks afterwards
cbar = fig.colorbar(cax, orientation='horizontal')
cbar.set_ticks(ticks=[-1, 0, 1], labels=['Low', 'Medium', 'High'])

plt.show()

# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.colorbar.Colorbar.set_ticks`
# - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`