1

I am quite new to Python Matplot. I am trying to plot a 3d bar plot in which the hight of the bar is z and the position of the bars are x,y of my array.

This is an example of my data, I have an 10*10 array:

data = np.array([[0.1729,0.8658,0.9283,0.9381,0.9535,1.0000,1.0000,0.9999,0.9999,1.0000],
[-0.0430,0.3109,0.2788,0.5846,0.5369,0.5717,0.6357,0.5619,0.5866,0.6587],
[-0.2228,0.3427,0.1842,0.2106,0.4398,0.3896,0.4687,0.5749,0.4684,0.5484],
[0.0011,0.2899,0.1854,0.1040,0.3513,0.3863,0.3971,0.4485,0.2514,0.4946],
[-0.0096,0.0630,0.1564,-0.0077,0.0825,0.1637,0.2627,0.4800,0.3931,0.3244],
[0.0134,0.1198,0.3339,0.1243,-0.0864,0.4048,0.1996,0.3988,0.1970,0.5248],
[-0.0153,0.2806,0.1926,0.1918,0.0860,0.1054,0.3202,0.3063,0.2644,0.3791],
[-0.1118,0.3065,0.1396,0.0949,0.3459,0.1168,0.3730,0.0741,0.3518,0.2150],
[0.1022,0.3144,-0.1165,0.0509,0.1462,0.3265,0.2087,0.2549,0.3914,0.2683],
[-0.0083,-0.0228,0.0721,0.1086,0.2400,0.1899,0.0922,0.1006,0.2430,0.2768]])

I am using the below code to generate the 3d bar plot :

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

fig = plt.figure()
ax1 = Axes3D(fig)

xpos = np.arange(data.shape[0])
ypos = np.arange(data.shape[1])
zpos = data

xposM, yposM = np.meshgrid(xpos, ypos, copy=False)

dx=0.5
dy=0.5
dz=zpos

ax1.bar3d(xposM.ravel(), yposM.ravel(), dz, dx, dy, dz)
plt.show()

but I am getting this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Does any body know what is the problem of my code?

2
  • numpy doesn't do plotting. You want matplot; here is an example of what you're looking for. Commented Feb 24, 2015 at 20:05
  • @Tom thanks. I have seen that and I could not figure out how that applies to a high dimensional matrix. Do not understand the elements in bar3d. Can you help me with my example matrix/.? Commented Feb 24, 2015 at 20:08

1 Answer 1

4

There are two issues with your code.

1: zpos should not be =data; this is the main hangup you are having.

2: You should (probably) be calling bar3d as ax1.bar3d(xposM.ravel(), yposM.ravel(), zpos, dx, dy, dz). As it stands, you are positioning your bars to have their base at where they should go. That is, a bar of height 3 is going to start at z=3 and go to z=6, and a bar of height -2.1 is going to start at z=-2.1.

As for the first issue, xpos and ypos have shape (100,), while data has shape (10,10). You need them to have the same dimensions, so I would recommend including zpos = np.zeros(len(data.ravel())). This also means you will need to set dz = data.ravel().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Tom. When I apply the correction you recommended, it takes a long time to run and no result yet. do you have any guess why?
I am not sure. Did you make any substitutions to your code other than: LINE 10: zpos = data becomes zpos = np.zeros(len(data.ravel())); LINE 16: dz=zpos becomes dz = data.ravel(); LINE 18: ax1.bar3d(xposM.ravel(), yposM.ravel(), dz, dx, dy, dz) becomes ax1.bar3d(xposM.ravel(), yposM.ravel(), zpos, dx, dy, dz)?

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.