与matplotlib堆叠的3d条形图 [英] Stacked 3d bar chart with matplotlib

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

问题描述

我使用以下代码制作了简单的3d条形图:

 来自mpl_toolkits.mplot3d import Axes3D 
导入matplotlib.pyplot as plt
导入numpy as np

图= plt.figure()
轴= fig.add_subplot(111,投影= 3d)

ax.set_xlabel( x)
ax.set_ylabel( y)
ax.set_zlabel( z)
ax.set_xlim3d(0,10)
ax.set_ylim3d(0,10)
ax.set_zlim3d(0,2)

xpos = [2,5,8,2,5,8,2,5, 8]
ypos = [1,1,1,5,5,5,9,9,9]
zpos = np.zeros(9)

dx = np .ones(9)
dy = np.ones(9)
dz = np.ones(9)

ax.bar3d(xpos,ypos,zpos,dx,dy ,dz)
plt.gca()。invert_xaxis()
plt.show()

作为测试的思考,到目前为止,一切似乎都还很清楚。我只是想知道我如何以堆叠的方式绘制这9条线的每条线,例如每个小节都分为4个部分,组成整个小节。



基本上,我想通过示例

  from mpl_toolkits.mplot3d import axes3D 
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig .add_subplot(111,projection = 3d)

ax.set_xlabel( x)
ax.set_ylabel( y)
ax.set_zlabel( z )
ax.set_xlim3d(0,10)
ax.set_ylim3d(0,10)

xpos = [2,5,8,2,5,8,2, 5,8]
ypos = [1,1,1,5,5,5,9,9,9]
zpos = np.zeros(9)

dx = np.ones(9)
dy = np.ones(9)
dz = [i在range(4)中的np.random.random(9)]#4个小节的高度

_zpos = zpos#每个条的起始zpos
颜色= ['r','b','g','y']
for i in range( 4):
ax.bar3d(xpos,ypos,_zpos,dx,dy,dz [i],color = colors [i])
_zpos + = dz [i]#加上每个的高度酒吧知道从哪里开始下一个

plt.gca()。invert_xaxis()
plt.show()


i worked on a simple 3d bar chart using the following code:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection = "3d")

ax.set_xlabel("x")
ax.set_ylabel("y") 
ax.set_zlabel("z")
ax.set_xlim3d(0,10)
ax.set_ylim3d(0,10) 
ax.set_zlim3d(0,2)

xpos = [2,5,8,2,5,8,2,5,8]
ypos = [1,1,1,5,5,5,9,9,9]
zpos = np.zeros(9)

dx = np.ones(9)
dy = np.ones(9)
dz = np.ones(9)

ax.bar3d(xpos, ypos, zpos, dx, dy, dz)
plt.gca().invert_xaxis()
plt.show()

Thinking of this just as a test, all seems to be clear so far. I just wondered how i can plot each of these 9 bars in a stacked way, so that e.g. each bar is divided in 4 parts that make up the whole bar.

Basically, im thinking of doing this in the way of the example here.

But instead of 2 stacks, i want to have 4. Any ideas how to proceed from the point i am now? Each hint would be so much appreciated.

Thanks!

edit: if i want to implement given values for each stacked bar, e.g:

...
z = [np.array([ 0.2, 0.6, 0.3, 0.6, 0.4, 0.3, 0.8, 0.5,  0.7]), 
     np.array([ 0.8, 0.4, 0.5, 0.2, 0.8, 0.7, 0.4, 0.2,  0.9]),
     np.array([ 0.1, 0.2, 0.4, 0.4, 0.2, 0.6, 0.3, 0.6,  0.9]),
     np.array([ 0.9, 0.5, 0.7, 0.2, 0.5, 0.6, 0.7, 0.9,  0.7])]
dz = [z for i in range(4)]
...

this doesnt seem to work and i dont know why?

解决方案

To make a stacked 3d bar plot, you can accumulate your dz values and use them as the base for each next bar. Here's an example:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection = "3d")

ax.set_xlabel("x")
ax.set_ylabel("y") 
ax.set_zlabel("z")
ax.set_xlim3d(0,10)
ax.set_ylim3d(0,10) 

xpos = [2,5,8,2,5,8,2,5,8]
ypos = [1,1,1,5,5,5,9,9,9]
zpos = np.zeros(9)

dx = np.ones(9)
dy = np.ones(9)
dz = [np.random.random(9) for i in range(4)]  # the heights of the 4 bar sets

_zpos = zpos   # the starting zpos for each bar
colors = ['r', 'b', 'g', 'y']
for i in range(4):
    ax.bar3d(xpos, ypos, _zpos, dx, dy, dz[i], color=colors[i])
    _zpos += dz[i]    # add the height of each bar to know where to start the next

plt.gca().invert_xaxis()
plt.show()

这篇关于与matplotlib堆叠的3d条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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