python - generating custom colorbar: match data with color scheme -


i have 3d bar chart, generated 3d discrete-dataset(x,y,z).
used code plot on canvas:

bar_chart_3d =fx.bar3d(x, y, z, dx, dy, dz, color=cmap.to_rgba(dz), alpha=0.4,linewidth=0.5)   

but, i'm not being able attach colormap on canvas based on dz value plotted.
tried using:

#adding color-bar... fig.colorbar(cmap.to_rgba(dz), shrink=0.6, aspect=8)    

but invoking attributeerror: 'numpy.ndarray' object has no attribute 'autoscale_none'.
thank you.

from bar3d docs, color applied each face,

color can be:

an array of colors of length n bars, color each bar independently.

i guess facecolor, not assumed convey information , bar3d not return anything. things contour return scalarmappable can used specify colorbar. looks cmap (which assume cm.scalarmappable?) doesn't contain norm , autoscale required colorbar.

as work around can draw colorbar between limits in example

fig.subplots_adjust(bottom=0.25) ax1 = fig.add_axes([0.05, 0.10, 0.9, 0.1]) norm = mpl.colors.normalize(vmin=dz.min(), vmax=dz.max()) cb1 = mpl.colorbar.colorbarbase(ax1, cmap=mpl.cm.cool,                                norm=norm,                                orientation='horizontal') 

where minimum , maximum dz specify range consistent data.

edit: added minimum working example specify colormap, use bar colors , add colormap bottom.

from mpl_toolkits.mplot3d import axes3d import matplotlib mpl import matplotlib.pyplot plt import numpy np  fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x, y = np.random.rand(2, 100) * 4 hist, xedges, yedges = np.histogram2d(x, y, bins=4)  elements = (len(xedges) - 1) * (len(yedges) - 1) xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)  xpos = xpos.flatten() ypos = ypos.flatten() zpos = np.zeros(elements) dx = 0.5 * np.ones_like(zpos) dy = dx.copy() dz = hist.flatten()  #define colormap , values barcolors cmap = plt.cm.rdylbu_r norm = mpl.colors.normalize(vmin=dz.min(), vmax=dz.max()) barcolors = plt.cm.scalarmappable(norm, cmap)  #plot bar3d ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=barcolors.to_rgba(dz),alpha=0.4,linewidth=0.5, zsort='average')  #add colorbar fig.subplots_adjust(bottom=0.25) ax1 = fig.add_axes([0.05, 0.10, 0.9, 0.1]) cb1 = mpl.colorbar.colorbarbase(ax1, cmap=cmap,                                    norm=norm,                                    orientation='horizontal') plt.show() 

which gives, enter image description here


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -