Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a 3d effect on bars in matplotlib?

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

like image 256
pleasedontbelong Avatar asked Aug 17 '10 11:08

pleasedontbelong


1 Answers

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

like image 66
doug Avatar answered Sep 29 '22 06:09

doug