-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New "extend" keyword to colors.BoundaryNorm #5034
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a87301
Solve conflicts
fmaussion a3e16b4
added support for extend in colorbarbase
fmaussion b2cfc86
won't override user extend keyword anymore
fmaussion 535bee8
Updates for pytest and mpl2
fmaussion 2a746cf
Add colorbar example to docs
fmaussion fe13320
what's new
fmaussion 75a0b7f
Update example
fmaussion 8e28cec
Review
fmaussion 4998329
Simplify BoundaryNorm calculation.
efiring 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 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,46 @@ | ||
New "extend" keyword to colors.BoundaryNorm | ||
------------------------------------------- | ||
|
||
:func:`~matplotlib.colors.BoundaryNorm` now has an ``extend`` kwarg. This is | ||
useful when creating a discrete colorbar from a continuous colormap: when | ||
setting ``extend`` to ``'both'``, ``'min'`` or ``'max'``, the colors are | ||
interpolated so that the extensions have a different color than the inner | ||
cells. | ||
|
||
Example | ||
``````` | ||
:: | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.colors import BoundaryNorm | ||
import numpy as np | ||
|
||
# Make the data | ||
dx, dy = 0.05, 0.05 | ||
y, x = np.mgrid[slice(1, 5 + dy, dy), | ||
slice(1, 5 + dx, dx)] | ||
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x) | ||
z = z[:-1, :-1] | ||
|
||
# Z roughly varies between -1 and +1 | ||
# my levels are chosen so that the color bar should be extended | ||
levels = [-0.8, -0.5, -0.2, 0.2, 0.5, 0.8] | ||
cmap = plt.get_cmap('PiYG') | ||
|
||
# Before this change | ||
plt.subplot(2, 1, 1) | ||
norm = BoundaryNorm(levels, ncolors=cmap.N) | ||
im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm) | ||
plt.colorbar(extend='both') | ||
plt.axis([x.min(), x.max(), y.min(), y.max()]) | ||
plt.title('pcolormesh with extended colorbar') | ||
|
||
# With the new keyword | ||
norm = BoundaryNorm(levels, ncolors=cmap.N, extend='both') | ||
plt.subplot(2, 1, 2) | ||
im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm) | ||
plt.colorbar() # note that the colorbar is updated accordingly | ||
plt.axis([x.min(), x.max(), y.min(), y.max()]) | ||
plt.title('pcolormesh with extended BoundaryNorm') | ||
|
||
plt.show() |
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
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
Binary file added
BIN
+16.4 KB
lib/matplotlib/tests/baseline_images/test_colors/boundarynorm_and_colorbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
This file was deleted.
Oops, something went wrong.
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.
This example is pretty long for a rarely used kwarg - suggest just linking the real example...
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.
Oh hmmm, there is no example. Suggest this gets put in the appropriate section of
examples/
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.
I still wonder how people are doing that without this kwarg, but well. ;-)
will do