Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

request for plotting variable bin size with imshow #1729

Closed
fkbreitl opened this Issue · 8 comments

4 participants

@fkbreitl

[Uploading hist2d.png . . .]()

As discussed at the SciPy developers mailing list there is no simple way to plot 2D histograms with variable bin size (http://mail.scipy.org/pipermail/scipy-dev/2013-January/018271.html). Therefore it was requested that imshow provides such a possibility. The initial email is posted blow:

Hello,

http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html

explains the usage of 2D histograms.
Although it is possible to construct 2D histograms with a variable bin
size, there is no simple way to plot them.
Therefore I would like to request an implementation to imshow that
allows the visualization of 2D histograms with variable bin size. I
imagine a syntax like

imshow(historgram2d, xedges=xedges, yedges=yedges)

where xedges and yedges are the bin edges along the x- and y-axis.

I have attached a short program and its output which constructs a
numpy.historgram2d with variable bin width (xedges=[0,1,3]) and shows
its bin content with imshow.
I would highly appreciate if imshow would be extended to represent the
histogram correctly.
If there is an alternative solution, I would be interested to learn
about it as well.

Kind regards

Frank

--hist2d.py--

import numpy as np, matplotlib.pyplot as plt

#x, y = np.random.randn(2, 10)
x=[2]
y=[0]
xedges=[0,1,3]
yedges=[0,1]

H, yedges, xedges = np.histogram2d(y,x, bins=(yedges,xedges))
extent = [xedges[0], xedges[-1], yedges[-1], yedges[0]]
plt.imshow(H, extent=extent, interpolation='None', aspect='auto')
plt.colorbar()
plt.show()

@tacaswell
Owner

You can do this with pcolor http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.pcolor

#x, y = np.random.randn(2, 10)
x=[2]
y=[0]
xedges=[0,1,3]
yedges=[0,1]

H, yedges, xedges = np.histogram2d(y,x, bins=(yedges,xedges))
extent = [xedges[0], xedges[-1], yedges[-1], yedges[0]]
X,Y = np.meshgrid(xedges, yedges)
plt.pcolor(X, Y, H)
plt.colorbar()
plt.show()


@fkbreitl

Thanks, in fact it plots the histogram correctly.
However, I forgot to mention is that for displaying dynamic radio spectra I would also need interpolation which imshow does very well and fast. I know that matplotlib also provides mlab.griddata. However this also involves rather intensive computations. Therefore for practical purposes it would be very useful to have this functionality in imshow.

@WeatherGod
Collaborator
@tacaswell
Owner

There also looks like there is a NonUniformImage class in image.py (https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/image.py#L667), that lets you specify the centers of the pixels, but it does not look like it is used anywhere in the code (and I have a bit of trouble sorting out how get it to work).

@fkbreitl

RectBivariateSpline() sounds interesting, if it is not too complicated to apply, e.g. to the example above.
NonUniformImages sounds even more in the right direction, if it could be made working.

@efiring
Owner
@fkbreitl

Oh, this example looks like it might be providing what I was looking for. What a pity I didn't find it earlier. It is probably because this example is completely isolated from the histogram2d examples at the Scipy page and there is also no reference from imshow. That could be an explanation why most people are not aware of it. Adding key words like histogram and variable bin width to the example could maybe make if visible through Google.

@fkbreitl

Ok, I worked out an example based on NonUniformImage and added it at
http://www.scipy.org/Cookbook/Histograms .
So since NonUniformImage provides an alternative solution, the feature requested here might not be necessary any more. Therefore I will close it now.

Frank

@fkbreitl fkbreitl closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.