是否可以在matplotlib中叠加3-d条形图? [英] Is it possible to superimpose 3-d bar charts in matplotlib?

查看:173
本文介绍了是否可以在matplotlib中叠加3-d条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是此版本的3d版本:在以下位置绘制两个直方图同时使用matplotlib

Essentially a 3d version of this: Plot two histograms at the same time with matplotlib

尽管我不知道该怎么做,因为我正在使用Axes 3d.

Though I don't know how to do it since I am using Axes 3d.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as 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 for i in xpos]
ypos = [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3]
ypos = [i+0.25 for i 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 for i in dz] # to 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()

推荐答案

已经解决了一半的问题,因为第一个棘手的问题是使用alpha=0.5将条形图设置为半透明.尽管还需要解决一个更细微的问题.

Half of the problem you have already figured out, since the first tricky bit is to set your bar plot to semi-transparent with alpha=0.5. There's a more subtle issue though that needs to be taken care of.

第一个天真的尝试是将对bar3d的调用复制到另一个数据集. 应该起作用,并且在某种程度上起作用.您的小节彼此完全重叠(在dz2中,非零全部包含在dz中的高小节中),所以我建议使较小的小条不太透明,而使较高的小条更加透明,如下所示:

The first naive try is to duplicate your call to bar3d to the other data set. This should work, and in a way it does. Your bars fully overlap with each other (in dz2 the nonzeros are all contained in highed bars in dz), so I suggest making the smaller bars less transparent, and the higher bars more transparent, like so:

# less transparent
ax1.bar3d(xpos, ypos, zpos, dx , dy ,dz,  color = '#ff0080', alpha = 0.3)
# use dz2, fully opaque, and bluish
ax1.bar3d(xpos, ypos, zpos, dx , dy ,dz2,  color = '#008080', alpha = 1)

但是,这会产生丑陋且不想要的结果,导致z==0处的某些面看起来呈现在z>0处的面之前.它可能特定于正在使用的后端:我正在使用Qt4Agg后端从ipython运行它.这也使我可以围绕图旋转,在这种情况下,很明显,这种方法存在致命的渲染问题.这是静止图像:

However, this produces the ugly and unwanted result that some of the faces at z==0 appear to be rendered in front of faces at z>0. It might be specific to the backend in use: I'm running this from ipython with the Qt4Agg backend. This also allows me to rotate around the plot, in which case it's obvious that there are fatal rendering problems with this approach. Here's a still image:

您可以在左侧第二个栏上看到一个零级补丁后面,该栏似乎在该栏顶部补丁的前面.显然,您(或任何人)不需要什么.

You can see on the second bar from the left that a zero-level patch behind the bar seems to be in front of the top patch of the bar. Obviously not what you (or anybody, for that matter) needs.

经过一些实验(以及此答案的有用提示),我意识到bar3d简直是越野车同时绘制多个条形图时.解决方法很简单:使用循环一个接一个地创建每个小节,问题(几乎全部)消失了:

After a bit of experimenting (and a helpful hint from this answer) I realized that bar3d is simply buggy when plotting multiple bars at the same time. The workaround is easy: use a loop to create each bar one by one, and the problem (almost entirely) goes away:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as 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 for i in xpos]
ypos = [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3]
ypos = [i+0.25 for i 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 for i in dz] # to superimpose dz

ticksx = np.arange(0.5,4,1)
ticksy = np.arange(0.6,4,1)

# only change here:
# new loop, changed alphas and a color
for 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 this plot around with an interactive backend, it's clear that it behaves almost perfectly (there are still minor glitches from certain viewing directions though). Here's a still from the fixed solution:

最后,请注意,即使没有渲染故障,也很难理解这些重叠的条形图.您在问题中链接的第二个案例可以避免,因为两个凸点明显分开.如果它们有很大的重叠,那么情节将很难理解.我建议考虑其他可视化方式,例如将每个条形图切成两段(每个都有一个垂直平面),并在每个位置并排绘制两组z数据.

Finally, note that even if there were no rendering glitches, it is not really easy to comprehend such overlapping bar plots. The 2d case you link to in your question can get away with it since the two bumps are clearly separated. If they were to have huge overlap, then the plot would be much harder to understand. I suggest to consider other ways of visualization, for instance cutting each bar into two (with a vertical plane each) and plotting the two sets of z data side-by-side at each position.

这篇关于是否可以在matplotlib中叠加3-d条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆