Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.2k
Add example to histogram colorbar on galleries#30107
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from30 commits
a49fac587e8e37901cfa64df2b39646be10c81072f19a2d199931dba578e4140089861bf6f2e89c5c85996781f36062d9169a388fb3d7b428004742f3e8acfcf4f62c767ec0efdb37d431cf67de19c7c6e02b014beda46aaf473b9a41d546685e2d109d65d508006643ee4a67File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||
| """ | ||||||
| ===================== | ||||||
| Histogram as colorbar | ||||||
| ===================== | ||||||
| This example demonstrates how to use a colored histogram instead of a colorbar | ||||||
| to not only show the color-to-value mapping, but also visualize the | ||||||
| distribution of values. | ||||||
| """ | ||||||
| import matplotlib.pyplot as plt | ||||||
| import numpy as np | ||||||
| import matplotlib.colors as mcolors | ||||||
| # surface data | ||||||
| delta = 0.025 | ||||||
| x = y = np.arange(-2.0, 2.0, delta) | ||||||
| X, Y = np.meshgrid(x, y) | ||||||
| Z1 = np.exp(-(((X + 1) * 1.3) ** 2) - ((Y + 1) * 1.3) ** 2) | ||||||
| Z2 = 2.5 * np.exp(-((X - 1) ** 2) - (Y - 1) ** 2) | ||||||
| Z = Z1**0.25 - Z2**0.5 | ||||||
| # colormap & normalization | ||||||
| bins = 30 | ||||||
| cmap = plt.get_cmap("RdYlBu_r") | ||||||
timhoffm marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||
| bin_edges = np.linspace(Z.min(), Z.max(), bins + 1) | ||||||
| norm = mcolors.BoundaryNorm(bin_edges, cmap.N) | ||||||
| # main plot | ||||||
| fig, ax = plt.subplots(layout="constrained") | ||||||
| im = ax.imshow(Z, cmap=cmap, origin="lower", extent=[-3, 3, -3, 3], norm=norm) | ||||||
| # inset histogram | ||||||
| cax = ax.inset_axes([1.18, 0.02, 0.25, 0.95]) # left, bottom, width, height | ||||||
| # plot histogram | ||||||
| counts, bin_edges = np.histogram(Z, bins=bins) | ||||||
| ||||||
| counts,bin_edges=np.histogram(Z,bins=bins) | |
| counts,_=np.histogram(Z,bins=bin_edges) |
Sorry, I just realized, we can make this now slightly safer/more obvious by using the existingbin_edges and not letnp.histogram create them anew, hoping they are identical to the explicit calculation above.
livlutz marked this conversation as resolved.Show resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.