I got a problem with producing a stacked 3D plot. I have three columns two are from the result = np.array(result) I want to stack add the weights array to each of the values in results in sequential order and stack them. I tried to follow the instructions as provided in this link: [https://stackoverflow.com/questions/35347091/python-3d-stacked-bar-char-plot][1] but the output is always faulty. The following shows the code and traceback
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
result = [[0.234, 0.309, 0.105, 0.367, 0.621],
[0.126, 0.286, 0.220, 0.367, 0.066]]
weight = [0.47, 0.21, 0.16, 0.11, 0.05]
weight = np.array(weight)
result = np.array(result)
fig = plt.figure(figsize=(10, 10), dpi=150)
ax1 = fig.add_subplot(111, projection='3d')
xlabels = np.array(['t1', 't2', 't3', 't4', 't5'])
xpos = np.arange(xlabels.shape[0])
ylabels = np.array(['ts1', 'ts2'])
ypos = np.arange(ylabels.shape[0])
xposM, yposM = np.meshgrid(xpos, ypos, copy=False)
zpos = result
zpos = zpos.ravel()
dx = 0.2
dy = 0.2
dz = zpos
ax1.w_xaxis.set_ticks(xpos + dx / 2.)
ax1.w_xaxis.set_ticklabels(xlabels)
ax1.w_yaxis.set_ticks(ypos + dy / 2.)
ax1.w_yaxis.set_ticklabels(ylabels)
values = np.linspace(0.2, 1., xposM.ravel().shape[0])
#create a subplot
dx = 0.2
dy = 0.2
dz2 = weight
# Set z position to the values of the first histogram
zpos2 = dz
ax1.bar3d(xpos, ypos, zpos, dx, dy, dz, color='lightcyan', zsort='average', alpha=0.7)
ax1.bar3d(xpos, ypos, zpos2, dx, dy, dz2, color='turquoise', zsort='average', alpha=0.7)
plt.show()
Traceback (most recent call last): File "C:\Users\k\PycharmProjects\app\Location_Decision_Framework.py", line 41, in ax1.bar3d(xpos, ypos, zpos, dx, dy, dz, color='lightcyan', zsort='average', alpha=0.7) File "C:\Users\k\PycharmProjects\app\venv\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 2541, in bar3d x, y, z, dx, dy, dz = np.broadcast_arrays( File "<array_function internals>", line 5, in broadcast_arrays File "C:\Users\k\PycharmProjects\app\venv\lib\site-packages\numpy\lib\stride_tricks.py", line 538, in broadcast_arrays shape = _broadcast_shape(*args) File "C:\Users\k\PycharmProjects\app\venv\lib\site-packages\numpy\lib\stride_tricks.py", line 420, in _broadcast_shape b = np.broadcast(*args[:32]) ValueError: shape mismatch: objects cannot be broadcast to a single shape