Best way to plot a 2d histogram?

Hello all,

What is the best way to plot a 2d histogram?
(Note that a 2d histogram is a histogram of a bivariate variable,
so it's got to be a 3d plot.)

Ideally, it should look somewhat like this:
http://www.desy.de/~mraue/public/rootTutorial/v0.2/histogram02.gif

For now, I have tried to do surface plots, one for each "bin",
but this way you only get the tops of a series of imaginary columns
and it looks a bit namby-pamby if you know what I mean.

Any idea will be appreciated.

···

--
Ernest

Although it is not an exact histogram, if you are you looking for a
Pythonic alternative you might consider using Mayavi. It has ready
barchart plotting functionality. Probably with some effort a 2D
histogram as you linked might be created.

···

On Thu, Oct 1, 2009 at 5:29 PM, Ernest Adrogué <eadrogue@...361...> wrote:

Hello all,

What is the best way to plot a 2d histogram?
(Note that a 2d histogram is a histogram of a bivariate variable,
so it's got to be a 3d plot.)

Ideally, it should look somewhat like this:
http://www.desy.de/~mraue/public/rootTutorial/v0.2/histogram02.gif

For now, I have tried to do surface plots, one for each "bin",
but this way you only get the tops of a series of imaginary columns
and it looks a bit namby-pamby if you know what I mean.

Any idea will be appreciated.

--
Ernest

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

--
Gökhan

Here is a snippet that might get you started:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
import mpl_toolkits.mplot3d as plt3

data = np.random.random((8,8))**4
cmap = cm.RdBu

fig = plt.figure()
ax = plt3.Axes3D(fig)

d = 0.1

w, h = data.shape
for x in range(w):
    for y in range(h):
        ax.bar3d([x+d], [y+d], [0], 1-d, 1-d, data[x,y], cmap(data[x,y]))
ax.set_zlim3d((0, 1))
plt.show()

Obviously, you should replace data with the actual data you want to
plot (maybe numpy can help with the histogramming), and use an
appropriate cmap for your data. Note that in this case the entire box
is a single color, not shaded up the side as in the example you
referenced; I actually like it this way but I also don't know how one
would do the shading in matplotlib. I'll admit it's rather silly to
have to create all these boxes individually, but that's the only way I
could see to color each box according to its height. The variable d
just puts gaps between boxes; you could set this to zero to make the
boxes adjacent.

I haven't figured out how to properly set the tick labels on the x and
y axis in a 3d plot like this. Also, there are often some z-order
errors with boxes occluding each other when they shouldn't. I don't
know the details of how these things work internally; maybe some
experts could weigh in here.

Cheers,
Matthew

···

On Thu, Oct 1, 2009 at 3:29 PM, Ernest Adrogué <eadrogue@...361...> wrote:

Hello all,

What is the best way to plot a 2d histogram?
(Note that a 2d histogram is a histogram of a bivariate variable,
so it's got to be a 3d plot.)

Ideally, it should look somewhat like this:
http://www.desy.de/~mraue/public/rootTutorial/v0.2/histogram02.gif

For now, I have tried to do surface plots, one for each "bin",
but this way you only get the tops of a series of imaginary columns
and it looks a bit namby-pamby if you know what I mean.

Any idea will be appreciated.

--
Ernest

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hi,

1/10/09 @ 19:23 (-0500), thus spake Gökhan Sever:

Although it is not an exact histogram, if you are you looking for a
Pythonic alternative you might consider using Mayavi. It has ready
barchart plotting functionality. Probably with some effort a 2D
histogram as you linked might be created.

Enthought Tool Suite :: Enthought, Inc.

Yes, it looks like it! I actually had already had a look at mayavi
but found it overly complicated for a simple histogram. If it has
simple bar chart functionality may be an option though.

Cheers.

···

--
Ernest

Hi,

1/10/09 @ 18:17 (-0700), thus spake Matthew Neeley:

Here is a snippet that might get you started:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
import mpl_toolkits.mplot3d as plt3

data = np.random.random((8,8))**4
cmap = cm.RdBu

fig = plt.figure()
ax = plt3.Axes3D(fig)

d = 0.1

w, h = data.shape
for x in range(w):
    for y in range(h):
        ax.bar3d([x+d], [y+d], [0], 1-d, 1-d, data[x,y], cmap(data[x,y]))
ax.set_zlim3d((0, 1))
plt.show()

Obviously, you should replace data with the actual data you want to
plot (maybe numpy can help with the histogramming), and use an
appropriate cmap for your data. Note that in this case the entire box
is a single color, not shaded up the side as in the example you
referenced; I actually like it this way but I also don't know how one
would do the shading in matplotlib. I'll admit it's rather silly to
have to create all these boxes individually, but that's the only way I
could see to color each box according to its height. The variable d
just puts gaps between boxes; you could set this to zero to make the
boxes adjacent.

I haven't figured out how to properly set the tick labels on the x and
y axis in a 3d plot like this. Also, there are often some z-order
errors with boxes occluding each other when they shouldn't. I don't
know the details of how these things work internally; maybe some
experts could weigh in here.

Great!! I like the shading this way, so no problem here.
Too bad about the rendering glitches though. Although, maybe that
can be sorted out by increasing the distance between boxes...? I will
experiment a little with this method of yours as it does basically what
I wanted. If everything fails I think I will finally use a 2-d plot
using a colour map to represent the height of bars,
as in here: http://www.davidbdean.com/wp-content/uploads/hist2d.png

Thanks a lot!

Bye.

···

--
Ernest