0

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

6
  • The obvious answer to your question is, and this is what the error message tells you, that you try to use arrays of different lengths when creating the graph. The parameters for bar3d can be either an array or a float (that internally will be converted to an array of the appropriate length). However, you cannot supply arrays of different lengths - in your case, x has 5 values, y 2, and z 10. Commented May 9, 2021 at 14:46
  • I assume you think that matplotlib can guess how to repeat x and y for the 10 z-values but the mapping is ambiguous. Therefore, neither matplotlib nor I can provide a solution with certainty how your final graph should look like. You should describe how the data in results and weight should be represented in your graph. Or maybe you will now be able to solve your problem based on these hints. Commented May 9, 2021 at 14:49
  • Many thanks for your answer already! Is there a way in which I can repeat my x value so that it fits with the z= 10? Commented May 9, 2021 at 18:35
  • Numpy has two main commands: np.repeat repeating each element of an array and np.tile repeating the array. You will easily find out how to combine them in your case to have the desired x-y combinations of your arrays. Commented May 9, 2021 at 18:49
  • np.tile worked! Many thanks! Based on your tile insight I was also able to find this stackoverflow: link for those with a similar problem! Commented May 9, 2021 at 19:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.