Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request for plotting variable bin size with imshow #1729

Closed
fkbreitl opened this issue Feb 1, 2013 · 8 comments
Closed

request for plotting variable bin size with imshow #1729

fkbreitl opened this issue Feb 1, 2013 · 8 comments

Comments

@fkbreitl
Copy link

fkbreitl commented Feb 1, 2013

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
Copy link
Member

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
Copy link
Author

fkbreitl commented Feb 1, 2013

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
Copy link
Member

I have found that scipy.interpolate.RectBivariateSpline() to be very fast
in re-gridding data, in case you are interested.

@tacaswell
Copy link
Member

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
Copy link
Author

fkbreitl commented Feb 1, 2013

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
Copy link
Member

efiring commented Feb 1, 2013

On 2013/02/01 11:19 AM, fkbreitl wrote:

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.

There is also the variation on NonUniformImage which is exposed by
Axes.pcolorfast. The difference is that the latter is based on color
rectangle boundaries, not centers, and does no interpolation, while the
former uses centers and can do bilinear interpolation. You can use it
already as-is, but it could be made easier to use by providing an Axes
method wrapper for it.

See http://matplotlib.org/examples/pylab_examples/image_nonuniform.html

Eric


Reply to this email directly or view it on GitHub
#1729 (comment).

@fkbreitl
Copy link
Author

fkbreitl commented Feb 1, 2013

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants