views:

133

answers:

3

Hi, I have a very simple basic bar's graphic like this one alt text

but i want to display the bars with some 3d effect, like this alt text

I just want the bars to have that 3d effect...my code is:

fig = Figure(figsize=(4.6,4))
ax1 = fig.add_subplot(111,ylabel="Valeur",xlabel="Code",autoscale_on=True)

width = 0.35
ind = np.arange(len(values))
rects = ax1.bar(ind, values, width, color='#A1B214')
ax1.set_xticks(ind+width)
ax1.set_xticklabels( codes )
ax1.set_ybound(-1,values[0] * 1.1)
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)

i've been looking in the gallery of matplotlib,tried a few things but i wasn't lucky, Any ideas? Thxs

+1  A: 

You might be able to work something out with mplot3d.

katrielalex
+1  A: 

As far as I know Matplotlib doesn't by design support features like the "3D" effect you just mentioned. I remember reading about this some time back. I don't know it has changed in the meantime.

See this discussion thread for more details.

Update

Take a look at John Porter's mplot3d module. This is not a part of standard matplotlib but a custom extension. Never used it myself so can't say much about its usefulness.

Manoj Govindan
thxs.. ill find out how to use it
pleasedontbelong
+4  A: 

I certainly understand your reason for needing a 3d bar plot; i suspect that's why they were created.

The libraries ('toolkits') in Matplotlib required to create 3D plots are not third-party libraries, etc., rather they are included in the base Matplotlib installation. (This is true for the current stable version, which is 1.0, though i don't believe it was for 0.98, so the change--from 'add-on' to part of the base install--occurred within the past year, i believe)

So here you are:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as PLT
import numpy as NP

fig = PLT.figure()
ax1 = fig.add_subplot(111, projection='3d')

xpos = NP.random.randint(1, 10, 10)
ypos = NP.random.randint(1, 10, 10)
num_elements = 10
zpos = NP.zeros(num_elements)
dx = NP.ones(10)
dy = NP.ones(10)
dz = NP.random.randint(1, 5, 10)

ax1.bar3d(xpos, ypos, zpos, dx, dy, dz, color='#8E4585')
PLT.show()

To create 3d bars in Maplotlib, you just need to do three (additional) things:

  1. import Axes3D from mpl_toolkits.mplot3d

  2. call the bar3d method (in my scriptlet, it's called by ax1 an instance of the Axes class). The method signature:

    bar3d(x, y, z, dy, dz, color='b', zsort="average", *args, **kwargs)

  3. pass in an additional argument to add_subplot, projection='3d'

alt text

doug
wow thxs a lot!.. i'll try to install the mpl_toolkits.mplot3d and then i'll try to see if its possible to do what i want
pleasedontbelong
this toolkit is part of the base matplotlib install now (i believe this started with 1.0, which is the current stable release), so either you already have it, or just upgrade Matplotlib to get it.
doug