Note

Go to the endto download the full example code.

Histogram as colorbar#

This example demonstrates how to use a colored histogram instead of a colorbarto not only show the color-to-value mapping, but also visualize thedistribution of values.

colorbar histogram
importmatplotlib.pyplotaspltimportnumpyasnpimportmatplotlib.colorsasmcolors# surface datadelta=0.025x=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 & normalizationbins=30cmap=plt.get_cmap("RdYlBu_r")bin_edges=np.linspace(Z.min(),Z.max(),bins+1)norm=mcolors.BoundaryNorm(bin_edges,cmap.N)# main plotfig,ax=plt.subplots(layout="constrained")im=ax.imshow(Z,cmap=cmap,origin="lower",extent=[-3,3,-3,3],norm=norm)# inset histogramcax=ax.inset_axes([1.18,0.02,0.25,0.95])# left, bottom, width, height# plot histogramcounts,_=np.histogram(Z,bins=bin_edges)midpoints=(bin_edges[:-1]+bin_edges[1:])/2distance=midpoints[1]-midpoints[0]cax.barh(midpoints,counts,height=0.8*distance,color=cmap(norm(midpoints)))# stylingcax.spines[:].set_visible(False)cax.set_yticks(bin_edges)cax.tick_params(axis="both",which="both",length=0)plt.show()

Total running time of the script: (0 minutes 1.025 seconds)

Gallery generated by Sphinx-Gallery