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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
MKesper
  • 456
  • 5
  • 16
  • 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. – tacaswell Aug 05 '13 at 13:26
  • Maybe you can use the steps outlined on [this website](http://www.jkwchui.com/2010/03/3d-bar-histogram-in-python/) or [this updated/additional information](http://www.jkwchui.com/2010/04/3d-histogram-in-python-2/) – Schorsch Aug 05 '13 at 19:42
  • Schorsch, thanks, I've been there but the code gets very brokenly displayed. :( – MKesper Aug 06 '13 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." – MKesper Aug 29 '13 at 13:15

1 Answers1

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

Greg
  • 11,654
  • 3
  • 44
  • 50
  • 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. ;) – MKesper Aug 06 '13 at 14:10