Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
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 from9 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 |
|---|---|---|
| @@ -5,3 +5,5 @@ Color | ||
| For a description of the colormaps available in Matplotlib, | ||
| see the :ref:`colormaps tutorial <tutorials-colors>`. | ||
| - colorbar_histogram.py: Example of a colorbar with a histogram inset. | ||
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """ | ||
| ========================= | ||
| Colorbar with Histogram | ||
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| ========================= | ||
| This example demonstrates how to create a colorbar for an image and | ||
| add a histogram of the data values alongside it. This is useful for | ||
| visualizing the distribution of values mapped to colors. | ||
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| """ | ||
| importnumpyasnp | ||
| importmatplotlib.pyplotasplt | ||
| importmatplotlib.colorsasmcolors | ||
| frommpl_toolkits.axes_grid1importmake_axes_locatable | ||
| # Generate random data | ||
| x=np.random.random((10,10)) | ||
| # Compute histogram | ||
| counts,bins=np.histogram(x) | ||
| # Set up colormap and normalization | ||
| cmap=plt.get_cmap('viridis') | ||
| norm=mcolors.BoundaryNorm(bins,cmap.N) | ||
timhoffm marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| fig,ax=plt.subplots() | ||
| im=ax.imshow(x,cmap=cmap,norm=norm) | ||
| cbar=plt.colorbar(im,ax=ax) | ||
| cbar.set_label('Value') | ||
| # Create an axes on the right side of ax. The width of cax will be 20% of ax and the padding between cax and ax will be fixed at 0.05 inch. | ||
Check failure on line 32 in galleries/examples/color/colorbar_histogram.py
| ||
| divider=make_axes_locatable(ax) | ||
| cax=divider.append_axes("right",size="20%",pad=0.05) | ||
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| # Plot histogram | ||
| midpoints=bins[:-1]+np.diff(bins)/2 | ||
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| cax.barh(midpoints,counts,height=np.diff(bins),color=cmap(norm(midpoints))) | ||
livlutz marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| cax.set_yticks(bins) | ||
| cax.set_xlabel('Count') | ||
| cax.set_ylabel('Value') | ||
| cax.invert_yaxis()# Optional: to match the orientation of imshow | ||
| plt.tight_layout() | ||
| plt.show() | ||
Uh oh!
There was an error while loading.Please reload this page.