5

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
pleasedontbelong
  • 19,542
  • 12
  • 53
  • 77
  • 1
    What exactly can 3D bars add beside confusion? As long as nobody forces you to make this "3D", please: leave it. – Frank Aug 17 '10 at 11:37
  • 6
    u_U i'm beeing forced by the one that signs my check – pleasedontbelong Aug 17 '10 at 11:48
  • You might be able to work something out with [`mplot3d`](http://matplotlib.sourceforge.net/examples/mplot3d/bars3d_demo.html#mplot3d-example-code-bars3d-demo-py). – Katriel Aug 17 '10 at 11:19

2 Answers2

11

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
  • 69,080
  • 24
  • 165
  • 199
  • 1
    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 Aug 17 '10 at 23:23
  • 2
    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 Aug 17 '10 at 23:39
1

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
  • 72,339
  • 21
  • 134
  • 141