在 matplotlib 中制作双面/双色 3d 条形图?

问题描述 投票:0回答:0
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# create some sample data
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
z1 = np.array([1, 2, 3])
z2 = np.array([4, 5, 6])
z3 = np.array([7, 8, 9])
color1 = np.array(['r', 'g', 'b'])
color2 = np.array(['y', 'm', 'c'])

# create a figure and a set of subplots
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# plot the first set of bars
for i in range(len(x)):
    ax.bar(x[i], z1[i], y[i], zdir='y', color=color1[i], alpha=0.8)
    ax.bar(x[i], z1[i] - z1[i], y[i], zdir='y', color=color2[i], alpha=0.8)

# plot the second set of bars
for i in range(len(x)):
    ax.bar(x[i], z2[i], y[i]+0.5, zdir='y', color=color1[i], alpha=0.8)
    ax.bar(x[i], z2[i] - z2[i], y[i]+0.5, zdir='y', color=color2[i], alpha=0.8)

# plot the third set of bars
for i in range(len(x)):
    ax.bar(x[i], z3[i], y[i]+1.0, zdir='y', color=color1[i], alpha=0.8)
    ax.bar(x[i], z3[i] - z3[i], y[i]+1.0, zdir='y', color=color2[i], alpha=0.8)

# set the axis labels and title
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Bar Chart')

# show the plot
plt.show()


在 Windows 11 (Python 3.10) 上的 VSCode 中运行此代码会导致以下错误:

回溯(最后一次通话): 文件“c:\Usersq68\Documents\delta-scan-master\delta-scan-master est val_test_display.py”,第 73 行,在 ax.bar(x[i], z1[i], y[i], zdir='y', color=color1[i], alpha=0.8) 文件“C:\Usersq68\Documents\delta-scan-master\delta-scan-master nv\lib\site-packages\matplotlib_init_.py”,第 1459 行,在内部 返回 func(ax, *map(sanitize_sequence, args), **kwargs) 文件“C:\Usersq68\Documents\delta-scan-master\delta-scan-master nv\lib\site-packages\mpl_toolkits\mplot3d xes3d.py”,第 2318 行,在栏中 zs = np.broadcast_to(zs, len(left)) TypeError: 'numpy.int32' 类型的对象没有 len()

我正在尝试创建一组 3d 条形图,其中每个条形图都是双色的(代表垂直轴上的两个不同值)。我不明白为什么错误在第 73 行,而不是出现 len() 函数的第 72 行。我读到的错误本质上是在说我在整数数据类型上调用长度函数 - 但 x 是(应该是?)上面声明的 numpy 数组。有任何想法吗?还是 i 的类型和 len(x) 返回的类型不匹配?

附言StackOverflow 的第一篇帖子,所以如果您需要更多信息或者我是否违反了一些礼节规则,请告诉我。

python matplotlib 3d bar-chart
© www.soinside.com 2019 - 2024. All rights reserved.