-
Notifications
You must be signed in to change notification settings - Fork 46
Clarifying how shared colorbars are created #310
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c061e7d
Clarified how `vmax` and `Normalize` are used when creating shared co…
1a7746a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ea33a5e
Added contourf example
0d0664d
Merge remote-tracking branch 'origin/colorbar' into colorbar
de8f449
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24387ff
Addressed Robert's suggestions
dc1e298
Addressed Max's suggestions
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 |
---|---|---|
|
@@ -426,11 +426,54 @@ | |
"id": "1662bf3c", | ||
r-ford marked this conversation as resolved.
Show resolved
Hide resolved
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. Agree with previous comment - maybe reword to "This functionality was utilized with the previous histogram exercise"? Reply via ReviewNB |
||
"metadata": {}, | ||
"source": [ | ||
"You may have noticed the input argument `hist1[3]` to `fig.colorbar`. To clarify, `hist1` is a tuple returned by `hist2d`, and `hist1[3]` returns a `matplotlib.collections.QuadMesh` that points to the colormap. \n", | ||
"\n", | ||
"You may have noticed the input argument `hist1[3]` to `fig.colorbar`. To clarify, `hist1` is a tuple returned by `hist2d`, and `hist1[3]` returns a `matplotlib.collections.QuadMesh` that points to the colormap for the first histogram. To make sure that both histograms are using the same colormap with the same range of values, `vmax` is set to 0.18 for both plots. This ensures that both histograms are using colormaps that represent values from 0 (the default for histograms) to 0.18. Because the same data is used for both plots, it doesn't matter whether we pass in `hist1[3]` or `hist2[3]` to `fig.colorbar`.\n", | ||
"Read more at the [`matplotlib.axes.Axes.hist2d` documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.hist2d.html)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "84c50862", | ||
"metadata": {}, | ||
"source": [ | ||
"Other kinds of plots can share colorbars too. A common use case is filled contour plots with shared colorbars for comparing data. `vmin` and `vmax` behave the same way for `contourf` as they do for `hist2d`. A downside to using the `vmin` and `vmax` kwargs when plotting two different datasets is that while the colormaps may be the same, the dataset with a smaller range of values won't show the full range of colors as seen below. Thus, it *does* matter in this particular example which output from `contourf` is used to make the colorbar." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "28d4cea3", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x2 = y2 = np.arange(-3, 3.01, 0.025)\n", | ||
"X2, Y2 = np.meshgrid(x2, y2)\n", | ||
"Z = np.sqrt(np.sin(X2) ** 2 + np.sin(Y2) ** 2)\n", | ||
"Z2 = np.sqrt(2 * np.cos(X2) ** 2 + 2 * np.cos(Y2) ** 2)\n", | ||
"\n", | ||
"fig, ax = plt.subplots(nrows=1, ncols=2, constrained_layout=True)\n", | ||
"c1 = ax[0].contourf(X2, Y2, Z, vmin=0, vmax=2)\n", | ||
"c2 = ax[1].contourf(X2, Y2, Z2, vmin=0, vmax=2)\n", | ||
"fig.colorbar(c1, ax=ax[0], location='bottom')\n", | ||
"fig.colorbar(c2, ax=ax[1], location='bottom')\n", | ||
"\n", | ||
"fig.suptitle('Shared colormaps on data with different ranges')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "5570ebb7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"fig, ax = plt.subplots(nrows=1, ncols=2, constrained_layout=True)\n", | ||
"c1 = ax[0].contourf(X2, Y2, Z, vmin=0, vmax=2)\n", | ||
"c2 = ax[1].contourf(X2, Y2, Z2, vmin=0, vmax=2)\n", | ||
"fig.colorbar(c2, ax=ax, location='bottom')\n", | ||
"\n", | ||
"fig.suptitle('Using the contourf output from the data with a wider range')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "92d072f8-7370-4ea5-92e0-4407cb5905bb", | ||
|
@@ -495,6 +538,17 @@ | |
"cbar = fig.colorbar(hist1[3], ax=ax, location='bottom')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ea7f200f", | ||
"metadata": {}, | ||
"source": [ | ||
"### The `Normalize` Class\n", | ||
"Note that both plots use the `norm` kwarg. The `Normalize` class linearly normalizes data into the [0, 1] interval. This is used to linearly map the colors in the colormap to the data from `vmin` to `vmax`. In fact, we used this functionality in the previous histogram exercise! The `vmin` and `vmax` kwargs for `hist2d` are simply passed into the `Normalize` function. When making a custom colormap, it is best to specify how you want the data normalized.\n", | ||
"\n", | ||
"For non-linear nomalization, check out this [Colormap Normalization tutorial](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html#)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e41f44e0-2c4f-4ce2-abe6-35d20b8c142e", | ||
|
@@ -628,7 +682,7 @@ | |
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.4" | ||
"version": "3.10.5" | ||
} | ||
}, | ||
"nbformat": 4, | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.