-
-
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
Colorbar only tut #8600
Changes from 1 commit
f8d96be
d6b49b9
1a2b58a
a875825
c7f582c
00e7d0f
3c19954
6ae3c6f
2996c49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
============================= | ||
Customized Colorbars Tutorial | ||
============================= | ||
|
||
This tutorial shows how to build colorbars without an attached mappable. | ||
|
||
""" | ||
|
||
############################################################################### | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a big deal but you technically don't need to be commenting out lines here, just extend the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed so as to have uniform format in all tutorials. |
||
# Customized Colorbars | ||
# ==================== | ||
# | ||
# `matplotlib.colorbar.ColorbarBase` derives from `ScalarMappable` and puts a | ||
# colorbar in specified axes, it is the base class with standalone colorbar | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the original is clearer:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed. |
||
# drawing functionality. It can be used as-is to make a colorbar for a given | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no dash in as is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dash removed. |
||
# 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 axis at position | ||
# [left, bottom, width, height] where all quantities are in fractions of figure | ||
# width and height. | ||
|
||
import matplotlib.pyplot as plt | ||
import matplotlib as mpl | ||
|
||
fig = plt.figure(figsize=(8, 3)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if you can get away with this since axes stuff isn't the point of this tutorial: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even figsize is not necessary so removed that too. |
||
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15]) | ||
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15]) | ||
ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15]) | ||
|
||
############################################################################### | ||
# 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 `ColorbarBase` and | ||
# specify axis, colormap, norm and orientation as parameters. Here we create | ||
# a basic continuous colorbar with ticks and labels. There are many more kwargs | ||
# which can be used to further modify the colorbar. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In these kinds of sentences I like to say something like "for more examples, see ". Something worth thinking of inserting where reasonable! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure where could it point to so gave a link to colorbar api. |
||
|
||
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 ListedColormap which generates | ||
# colormap from a set of listed colors, a BoundaryNorm which generates a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. generates a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
# colormap index based on discrete interval and extended ends to show the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discrete intervals There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
# "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 ListedColormap is used, the length of the bounds array must be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ":class: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (and elsewhere in any point that we refer to a specific class / function / module in MPL) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
# 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 | ||
# ColorBase. 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(['r', 'g', 'b', 'c']) | ||
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 | ||
# -------------------------------------- | ||
# | ||
# Now in the third example we illustrate the use of custom length colorbar | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we illustrate the use of custom length colorbar extensions. (They're used on both discrete and continuous colorbars.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am guessing here that 'Now in the third example' is needed to be replaced by 'Here'. Correct me if I am wrong. |
||
# extensions, used on a colorbar with discrete intervals. Here we pass colors | ||
# as RGB triplet. To make the length of each extension the same as the length | ||
# of the interior colors pass extendfrac argument as auto | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pass the extendfrac argument There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of passing color in RGB format, I feel it will be more clear if colors are referenced by name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agree. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the name of the color with RGB = (0, 0.4, 1)? |
||
[1., .8, 0.], [1., .4, 0.]]) | ||
cmap.set_over((1., 0., 0.)) | ||
cmap.set_under((0., 0., 1.)) | ||
|
||
bounds = [-1., -.5, 0., .5, 1.] | ||
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.show() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stupid question but is there an extra line at the bottom? I think that's best practices but not sure what MPL does as a standard. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing it out. My text editor was inserting a new line on save, fixed now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a newline at the end. If GitHub has that little red symbol on the last line, it means there isn't one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops - yes I meant we wanted the new line, sorry about the confusion There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed now. |
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.
Since this is a tutorial, use something less jargony than mappable. Without an attached plot maybe?
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.
Mappable replaced by plot.