Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

DOC: Clarify/simplify example of multiple images with one colorbar#28546

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
timhoffm merged 2 commits intomatplotlib:mainfromtimhoffm:doc-multi-image
Jul 31, 2024
Merged
Changes fromall 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
88 changes: 51 additions & 37 deletionsgalleries/examples/images_contours_and_fields/multi_image.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
"""
===============
Multiple images
===============
=================================
Multiple images with one colorbar
=================================
Make a set of images with a single colormap, norm, and colorbar.
Use a single colorbar for multiple images.
Currently, a colorbar can only be connected to one image. The connection
guarantees that the data coloring is consistent with the colormap scale
(i.e. the color of value *x* in the colormap is used for coloring a data
value *x* in the image).
If we want one colorbar to be representative for multiple images, we have
to explicitly ensure consistent data coloring by using the same data
normalization for all the images. We ensure this by explicitly creating a
``norm`` object that we pass to all the image plotting methods.
"""

importmatplotlib.pyplotasplt
Expand All@@ -12,47 +22,53 @@
frommatplotlibimportcolors

np.random.seed(19680801)
Nr=3
Nc=2

fig,axs=plt.subplots(Nr,Nc)
datasets= [
(i+1)/10*np.random.rand(10,20)
foriinrange(4)
]

fig,axs=plt.subplots(2,2)
fig.suptitle('Multiple images')

images= []
foriinrange(Nr):
forjinrange(Nc):
# Generate data with a range that varies from one plot to the next.
data= ((1+i+j)/10)*np.random.rand(10,20)
images.append(axs[i,j].imshow(data))
axs[i,j].label_outer()
# create a single norm to be shared across all images
norm=colors.Normalize(vmin=np.min(datasets),vmax=np.max(datasets))

# Find the min and max of all colors for use in setting the color scale.
vmin=min(image.get_array().min()forimageinimages)
vmax=max(image.get_array().max()forimageinimages)
norm=colors.Normalize(vmin=vmin,vmax=vmax)
foriminimages:
im.set_norm(norm)
images= []
forax,datainzip(axs.flat,datasets):
images.append(ax.imshow(data,norm=norm))

fig.colorbar(images[0],ax=axs,orientation='horizontal',fraction=.1)


# Make images respond to changes in the norm of other images (e.g. via the
# "edit axis, curves and images parameters" GUI on Qt), but be careful not to
# recurse infinitely!
defupdate(changed_image):
foriminimages:
if (changed_image.get_cmap()!=im.get_cmap()
orchanged_image.get_clim()!=im.get_clim()):
im.set_cmap(changed_image.get_cmap())
im.set_clim(changed_image.get_clim())


foriminimages:
im.callbacks.connect('changed',update)

plt.show()

# %%
# The colors are now kept consistent across all images when changing the
# scaling, e.g. through zooming in the colorbar or via the "edit axis,
# curves and images parameters" GUI of the Qt backend. This is sufficient
# for most practical use cases.
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# curves and images parameters" GUI of the Qt backend. This is sufficient
# for most practical use cases.
# curves and images parameters" GUI of the Qt backend.

Folks will tell us if it's sufficient?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I see this as guidance and affirmation that most users can stop here. They don't have to inverst the following extra effort for cmap handling.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, maybe "this is" is just ambiguous pronoun reference then.

#
# Advanced: Additionally sync the colormap
# ----------------------------------------
#
# Sharing a common norm object guarantees synchronized scaling because scale
# changes modify the norm object in-place and thus propagate to all images
# that use this norm. This approach does not help with synchronizing colormaps
# because changing the colormap of an image (e.g. through the "edit axis,
# curves and images parameters" GUI of the Qt backend) results in the image
# referencing the new colormap object. Thus, the other images are not updated.
#
# To update the other images, sync the
# colormaps using the following code::
#
# def sync_cmaps(changed_image):
# for im in images:
# if changed_image.get_cmap() != im.get_cmap():
# im.set_cmap(changed_image.get_cmap())
#
# for im in images:
# im.callbacks.connect('changed', sync_cmaps)
#
#
# .. admonition:: References
#
Expand All@@ -63,6 +79,4 @@ def update(changed_image):
# - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`
# - `matplotlib.colors.Normalize`
# - `matplotlib.cm.ScalarMappable.set_cmap`
# - `matplotlib.cm.ScalarMappable.set_norm`
# - `matplotlib.cm.ScalarMappable.set_clim`
# - `matplotlib.cbook.CallbackRegistry.connect`

[8]ページ先頭

©2009-2025 Movatter.jp