使用 matplotlib 堆叠 3d 条形图 [英] Stacked 3d bar chart with matplotlib
问题描述
我使用以下代码制作了一个简单的 3d 条形图:
from mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 plt将 numpy 导入为 npfig = plt.figure()ax = 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 导入 Axes3D导入 matplotlib.pyplot 作为 plt将 numpy 导入为 npfig = plt.figure()ax = 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)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)] # 4 个条形集的高度_zpos = zpos # 每个柱的起始 zpos颜色 = ['r', 'b', 'g', 'y']对于范围内的 i (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屋!