# [SciPy-user] how to plot the result of histogram2d

thanks Johan,
I posted to scipy because of histogram2d being in numpy, sorry about that.
Now the stupid question, why can't imshow directly parse histogram2d, without the transitory extent object?
Anyway, I am happy that there is a simple way as explained below!

thanks again,
Johann

John Hunter wrote:

···

On Fri, Sep 5, 2008 at 10:36 AM, Johann Cohen-Tanugi > <cohen@...1565...> wrote:

hi, I hope someone can quickly point me to some doc.
I can do imshow(histogram2d(x,y)[0]) but then I miss the x and y binning
correct labels.
If I do imshow(histogram2d(x,y)) I get:
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (115, 0))

matplotlib questions are best addressed to the matplotlib-users mailing list at

histogram2d returns H, xedges and yedges. The first argument should
be passed to imshow, and the second two can be used to get the extents

In [26]: x, y = np.random.randn(2, 100000)

In [27]: H, xedges, yedges = np.histogram2d(x, y, bins=50)

In [28]: extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

In [29]: imshow(H, extent=extent)
Out[29]: <matplotlib.image.AxesImage object at 0x9377bcc>

I
_______________________________________________
SciPy-user mailing list
SciPy-user@...177...
http://projects.scipy.org/mailman/listinfo/scipy-user

Johann Cohen-Tanugi wrote:

thanks Johan,
I posted to scipy because of histogram2d being in numpy, sorry about that.
Now the stupid question, why can't imshow directly parse histogram2d, without the transitory extent object?

imshow is a general image display function; it would not make sense to customize it to match histogram2d.

You may want to use Axes.pcolorfast instead. Example using ipython -pylab:

In [1]:import numpy as np

In [2]:x, y = np.random.randn(2, 100000)

In [3]:H, xedges, yedges = np.histogram2d(x, y, bins=50)

In [4]:fig = figure()

In [6]:ax.pcolorfast?

In [7]:ax.pcolorfast(xedges, yedges, H)
Out[7]:<matplotlib.image.AxesImage object at 0x8bf0ecc>

In [8]:draw()

Eric

···

Anyway, I am happy that there is a simple way as explained below!

thanks again,
Johann

John Hunter wrote:

On Fri, Sep 5, 2008 at 10:36 AM, Johann Cohen-Tanugi >> <cohen@...1565...> wrote:

hi, I hope someone can quickly point me to some doc.
I can do imshow(histogram2d(x,y)[0]) but then I miss the x and y binning
correct labels.
If I do imshow(histogram2d(x,y)) I get:
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (115, 0))

matplotlib questions are best addressed to the matplotlib-users mailing list at

histogram2d returns H, xedges and yedges. The first argument should
be passed to imshow, and the second two can be used to get the extents

In [26]: x, y = np.random.randn(2, 100000)

In [27]: H, xedges, yedges = np.histogram2d(x, y, bins=50)

In [28]: extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

In [29]: imshow(H, extent=extent)
Out[29]: <matplotlib.image.AxesImage object at 0x9377bcc>

thanks Eric! Could you provide me with an executive summary as to pcolorfast vs imshow? Is it essentially a matter of speed?
Also, I tried to add a colorbar but failed. What is the correct invocation?
thanks a gain,
Johann

Eric Firing wrote:

···

Johann Cohen-Tanugi wrote:

thanks Johan,
I posted to scipy because of histogram2d being in numpy, sorry about that.
Now the stupid question, why can't imshow directly parse histogram2d, without the transitory extent object?

imshow is a general image display function; it would not make sense to customize it to match histogram2d.

You may want to use Axes.pcolorfast instead. Example using ipython -pylab:

In [1]:import numpy as np

In [2]:x, y = np.random.randn(2, 100000)

In [3]:H, xedges, yedges = np.histogram2d(x, y, bins=50)

In [4]:fig = figure()

In [6]:ax.pcolorfast?

In [7]:ax.pcolorfast(xedges, yedges, H)
Out[7]:<matplotlib.image.AxesImage object at 0x8bf0ecc>

In [8]:draw()

Eric

Anyway, I am happy that there is a simple way as explained below!

thanks again,
Johann

John Hunter wrote:

On Fri, Sep 5, 2008 at 10:36 AM, Johann Cohen-Tanugi >>> <cohen@...1565...> wrote:

hi, I hope someone can quickly point me to some doc.
I can do imshow(histogram2d(x,y)[0]) but then I miss the x and y binning
correct labels.
If I do imshow(histogram2d(x,y)) I get:
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (115, 0))

matplotlib questions are best addressed to the matplotlib-users mailing list at

histogram2d returns H, xedges and yedges. The first argument should
be passed to imshow, and the second two can be used to get the extents

In [26]: x, y = np.random.randn(2, 100000)

In [27]: H, xedges, yedges = np.histogram2d(x, y, bins=50)

In [28]: extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

In [29]: imshow(H, extent=extent)
Out[29]: <matplotlib.image.AxesImage object at 0x9377bcc>

Johann Cohen-Tanugi wrote:

thanks Eric! Could you provide me with an executive summary as to pcolorfast vs imshow? Is it essentially a matter of speed?

It is more generality than speed.

imshow is for genuine image data: an array of pixel values, with the assumption that the pixels are square. imshow supports interpolation.

pcolorfast works for any rectilinear grid, and depending on the characteristics of that grid, it uses the same underlying code as imshow, or a slightly slower but more general code for irregular rectangular grids (NonUniformImage), or the considerably slower quadmesh code if the grid is not rectangular. Detection of the kind of grid, and therefore the algorithm to use, is based mainly on the presence and dimensionality of the X and Y input arrays.

pcolorfast does not support interpolation; it always displays quadrilaterals of uniform color.

pcolorfast differs from pcolor and pcolormesh in that it does not support drawing grid boundaries, it is much faster with *agg backends, and there are some differences in the way input arguments are handled. Also, there is as yet no pyplot interface to it; I thought that some api changes might still be in order, and do not want to put pcolorfast into pyplot until its api is stable.

Also, I tried to add a colorbar but failed. What is the correct invocation?

Here is one way:

import numpy as np
x, y = np.random.randn(2, 100000)
H, xedges, yedges = np.histogram2d(x, y, bins=50)
fig = figure()