-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Colorbar only tut #8600
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
Colorbar only tut #8600
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f8d96be
Convert colorbar_only example to tutorial
patniharshit d6b49b9
Move to tutorial directory
patniharshit 1a2b58a
Changes in colorbar_only tutorial documentation
patniharshit a875825
Changes to colorbar_only tutorial
patniharshit c7f582c
Change how kwarg extendfrac is presented in colorbar_only tutorial
patniharshit 00e7d0f
Add double backticks to extendfrac in colorbar_only tutorial
patniharshit 3c19954
Add new line at the end of the file
patniharshit 6ae3c6f
Call for new figure in each rst block
patniharshit 2996c49
Remove a line asking to create three axes in colorbar_only tutorial
patniharshit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
""" | ||
============================= | ||
Customized Colorbars Tutorial | ||
============================= | ||
|
||
This tutorial shows how to build colorbars without an attached plot. | ||
|
||
Customized Colorbars | ||
==================== | ||
|
||
:class:`~matplotlib.colorbar.ColorbarBase` derives from | ||
:mod:`~matplotlib.cm.ScalarMappable` and puts a colorbar in a specified axes, | ||
so it has everything needed for a standalone colorbar. It can be used as is to | ||
make a colorbar for a given colormap and does not need a mappable object like | ||
an image. In this tutorial we will explore what can be done with standalone | ||
colorbar. | ||
|
||
We will start by making a figure of desired size and adding three axes. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import matplotlib as mpl | ||
|
||
fig, (ax1, ax2, ax3) = plt.subplots(nrows=3) | ||
|
||
############################################################################### | ||
# Basic continuous colorbar | ||
# ------------------------- | ||
# | ||
# Set the colormap and norm to correspond to the data for which the colorbar | ||
# will be used. Then create the colorbar by calling | ||
# :class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm | ||
# and orientation as parameters. Here we create a basic continuous colorbar | ||
# with ticks and labels. More information on colorbar api can be found | ||
# `here <https://matplotlib.org/api/colorbar_api.html>`. | ||
|
||
cmap = mpl.cm.cool | ||
norm = mpl.colors.Normalize(vmin=5, vmax=10) | ||
|
||
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap, | ||
norm=norm, | ||
orientation='horizontal') | ||
cb1.set_label('Some Units') | ||
|
||
############################################################################### | ||
# Discrete intervals colorbar | ||
# --------------------------- | ||
# | ||
# The second example illustrates the use of a | ||
# :class:`~matplotlib.colors.ListedColormap` which generates a colormap from a | ||
# set of listed colors, :func:`colors.BoundaryNorm` which generates a colormap | ||
# index based on discrete intervals and extended ends to show the "over" and | ||
# "under" value colors. Over and under are used to display data outside of the | ||
# normalized [0,1] range. Here we pass colors as gray shades as a string | ||
# encoding a float in the 0-1 range. | ||
# | ||
# If a :class:`~matplotlib.colors.ListedColormap` is used, the length of the | ||
# bounds array must be one greater than the length of the color list. The | ||
# bounds must be monotonically increasing. | ||
# | ||
# This time we pass some more arguments in addition to previous arguments to | ||
# :class:`~matplotlib.colorbar.ColorbarBase`. For the out-of-range values to | ||
# display on the colorbar, we have to use the *extend* keyword argument. To use | ||
# *extend*, you must specify two extra boundaries. Finally spacing argument | ||
# ensures that intervals are shown on colorbar proportionally. | ||
|
||
cmap = mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan']) | ||
cmap.set_over('0.25') | ||
cmap.set_under('0.75') | ||
|
||
bounds = [1, 2, 4, 7, 8] | ||
norm = mpl.colors.BoundaryNorm(bounds, cmap.N) | ||
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap, | ||
norm=norm, | ||
boundaries=[0] + bounds + [13], | ||
extend='both', | ||
ticks=bounds, | ||
spacing='proportional', | ||
orientation='horizontal') | ||
cb2.set_label('Discrete intervals, some other units') | ||
|
||
############################################################################### | ||
# Colorbar with custom extension lengths | ||
# -------------------------------------- | ||
# | ||
# Here we illustrate the use of custom length colorbar extensions, used on a | ||
# colorbar with discrete intervals. To make the length of each extension same | ||
# as the length of the interior colors, use ``extendfrac='auto'``. | ||
|
||
cmap = mpl.colors.ListedColormap(['royalblue', 'cyan', | ||
'yellow', 'orange']) | ||
cmap.set_over('red') | ||
cmap.set_under('blue') | ||
|
||
bounds = [-1.0, -0.5, 0.0, 0.5, 1.0] | ||
norm = mpl.colors.BoundaryNorm(bounds, cmap.N) | ||
cb3 = mpl.colorbar.ColorbarBase(ax3, cmap=cmap, | ||
norm=norm, | ||
boundaries=[-10] + bounds + [10], | ||
extend='both', | ||
extendfrac='auto', | ||
ticks=bounds, | ||
spacing='uniform', | ||
orientation='horizontal') | ||
cb3.set_label('Custom extension lengths, some other units') | ||
|
||
plt.tight_layout() | ||
plt.show() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually do you think you could make this triple backticks?
...JK, this PR looks good to me :)