2

I'd like to prepare some statistics for my boss. The flat style of matplotlib bar chart would make them look cheap for those used to Excel charts, although for clarity, using styles like this probably should be avoided.

I'm not that far away, but I don't get how to give the right thickness of the bars:

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

row = [0, 0, 0, 22, 0, 0, 4, 16, 2, 0, 4, 4, 12, 26]
length = len(row)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(length)
y = np.zeros(14)
z = np.array(row)
width = 0.8
ax.bar3d(x, y, [0]*length, 0.5, 0.001, z)
ax.set_xticks(x + width/2)
ax.set_xticklabels(titles[2:], rotation=90)
ax.set_yticks(y)
ax.set_zlabel('count')
plt.show()

Result:Result

4
  • 3
    What have you tried? There was a question about cylinders for bars recently. Curious comparison to Excel though, I have the exact opposite reaction. Commented Aug 5, 2013 at 13:26
  • Maybe you can use the steps outlined on this website or this updated/additional information Commented Aug 5, 2013 at 19:42
  • Schorsch, thanks, I've been there but the code gets very brokenly displayed. :( Commented Aug 6, 2013 at 10:09
  • Web convinces me using this is no good idea after all: For example github.com/propublica/guides/blob/master/news-apps.md "Avoid 3-D charts at all costs." Commented Aug 29, 2013 at 13:15

1 Answer 1

4

The thickness of the bars are set by the dx, dy arguments in ax.bar3d for which you have the values 0.5, 0.001. The issue, as I'm sure you noticed is that changing dy will change the length of the bar (in your case the untitled axis), but matplotlib helpfully rescales the y axis so the data fills it. This makes it look strange (I am assuming this is the problem, sorry if it isn't).

To remedy this you could set the y limits using ax.set_ylim(0, 0.002) (basically your y values go from 0->0.001). If you change either dy or the value of y given to bar3d which is currently 0, then you will need to update the limits accordingly.

Example:

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

row = [0, 0, 0, 22, 0, 0, 4, 16, 2, 0, 4, 4, 12, 26]
length = len(row)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.bar3d(range(length), [0]*length, [0]*length, 0.5, 0.001, row)

ax.set_ylim(-0.005, 0.005)
plt.show()

Example

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

1 Comment

Thanks a lot! I'd have to put a lot of guesswork into that. Making the y ticks practically invisible didn't help me, either. ;)

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.