python - Is it possible to superimpose 3-d bar charts in matplotlib? -
essentially 3d version of this: plot 2 histograms @ same time matplotlib
though don't know how since using axes 3d.
from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot plt import numpy np kets = ["|00>","|01>","|10>","|11>"] #my axis labels fig = plt.figure() ax1 = fig.add_subplot(111,projection = '3d') xpos = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3] xpos = [i+0.25 in xpos] ypos = [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3] ypos = [i+0.25 in ypos] zpos = [0]*16 dx = 0.5*np.ones(16) dy = 0.5*np.ones(16) dz = [1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0] dz2 = [0.2*i in dz] # superimpose dz ticksx = np.arange(0.5,4,1) ticksy = np.arange(0.6,4,1) ax1.bar3d(xpos, ypos, zpos, dx , dy ,dz, color = '#ff0080', alpha = 0.5) ax1.w_xaxis.set_ticklabels(kets) ax1.w_yaxis.set_ticklabels(kets) ax1.set_zlabel('coefficient') plt.xticks(ticksx,kets) plt.yticks(ticksy,kets) plt.show()
half of problem have figured out, since first tricky bit set bar plot semi-transparent alpha=0.5. there's more subtle issue though needs taken care of.
the first naive try duplicate call bar3d other data set. should work, , in way does. bars overlap each other (in dz2 nonzeros contained in highed bars in dz), suggest making smaller bars less transparent, , higher bars more transparent, so:
# less transparent ax1.bar3d(xpos, ypos, zpos, dx , dy ,dz, color = '#ff0080', alpha = 0.3) # use dz2, opaque, , bluish ax1.bar3d(xpos, ypos, zpos, dx , dy ,dz2, color = '#008080', alpha = 1) however, produces ugly , unwanted result of faces @ z==0 appear rendered in front of faces @ z>0. might specific backend in use: i'm running ipython qt4agg backend. allows me rotate around plot, in case it's obvious there fatal rendering problems approach. here's still image:
you can see on second bar left zero-level patch behind bar seems in front of top patch of bar. not (or anybody, matter) needs.
after bit of experimenting (and helpful hint this answer) realized bar3d buggy when plotting multiple bars @ same time. workaround easy: use loop create each bar 1 one, , problem (almost entirely) goes away:
from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot plt import numpy np kets = ["|00>","|01>","|10>","|11>"] #my axis labels fig = plt.figure() ax1 = fig.add_subplot(111,projection = '3d') xpos = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3] xpos = [i+0.25 in xpos] ypos = [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3] ypos = [i+0.25 in ypos] zpos = [0]*16 dx = 0.5*np.ones(16) dy = 0.5*np.ones(16) dz = [1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0] dz2 = [0.2*i in dz] # superimpose dz ticksx = np.arange(0.5,4,1) ticksy = np.arange(0.6,4,1) # change here: # new loop, changed alphas , color k in range(len(xpos)): ax1.bar3d(xpos[k], ypos[k], zpos[k], dx[k] , dy[k] ,dz[k], color = '#ff0080', alpha = 0.3) ax1.bar3d(xpos[k], ypos[k], zpos[k], dx[k] , dy[k] ,dz2[k], color = '#008080', alpha = 1) ax1.w_xaxis.set_ticklabels(kets) ax1.w_yaxis.set_ticklabels(kets) ax1.set_zlabel('coefficient') plt.xticks(ticksx,kets) plt.yticks(ticksy,kets) plt.show() when rotating plot around interactive backend, it's clear behaves (there still minor glitches viewing directions though). here's still fixed solution:
finally, note if there no rendering glitches, not easy comprehend such overlapping bar plots. 2d case link in question can away since 2 bumps separated. if have huge overlap, plot harder understand. suggest consider other ways of visualization, instance cutting each bar 2 (with vertical plane each) , plotting 2 sets of z data side-by-side @ each position.


Comments
Post a Comment