error in hexbin normalization

Mike Bauer wrote:


Here's an example of a working hexbin (attached). What I want to do is compare this with another dataset with many fewer points. What I'd really like is for the color bar to reflect the cumulative percent of the total count each cell holds, but I'd settle for what I thought normalized gives which is scaling the colors from 0 - 1 instead of showing the number count. I don't care about comparing numbers I care about the relative frequency of each cell.

I don't have a solution for you, but it looks to me like you can do the sort of thing you are looking for via suitable choice of the C and reduce_C_function kwargs to hexbin. This is not a job for the norm kwarg.

Actually, here is a stab at what I think you are describing:

x = np.random.normal(size=(10000,))
y = np.random.normal(size=(10000,))
imask = (x > -1) & (x < 1) & (y > -1) & (y < 1)
x = x[imask]
y = y[imask]
c = np.ones_like(x) * 100 / len(x)
hexbin(x, y, C=c, reduce_C_function=np.sum, gridsize=20)

I think this is giving percentage of hits in each bin. The numbers are very small because there are many bins.



Thanks for the pointer to colors.LogNorm(). I'll look into that.


Here's my script (sorry, you'll see it's a temporary hack).

On Mar 20, 2009, at 7:10 PM, Eric Firing wrote:

Mike Bauer wrote:

Thanks for the reply. I'm trying to show the relative 2d distribuion between 2 sets of data. I thought the normalization would ease the comparison. Fixing the ' doesn't help.
So are you saying I need an instance of something.normalize rather than just passing norm='normalize'?

It sounds like you are misunderstanding the norm kwarg; it is for controlling the mapping of an arbitrary range of numbers to the 0-1 range that is used in color mapping. The default is a linear mapping; one can use a log mapping instead ("norm=colors.LogNorm()"), or make your own mapping function, etc. The norm kwarg takes an instance of a Normalize class or subclass. See to find out what Normalize subclasses are available. But, you may not need to specify one at all, depending on what it is you are trying to do.

I still don't understand what it is that you wanted to "normalize". What was the undesirable characteristic of the plot you had before you put in the norm kwarg?


Sent from my iPhone
On Mar 20, 2009, at 5:15 PM, Eric Firing <efiring@...202...> wrote:

Mike Bauer wrote:

Quick note. I'm making plots with hexbin and everything works correctly until I try to use the norm='Normalize' option at which point I get:
Traceback (most recent call last):
File "", line 731, in <module>
File "", line 605, in main
   plt.hexbin(xdat,ydat,cmap=cm.jet,gridsize=25,norm=Normalize' )

What is that single quote mark doing after Normalize? If we ignore it, then it looks like you are passing a class, not a class instance as the kwarg needs.

File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/ lib/python2.5/site-packages/matplotlib/", line 1920, in hexbin
   ret = gca().hexbin(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/ lib/python2.5/site-packages/matplotlib/", line 5452, in hexbin
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.5/ lib/python2.5/site-packages/matplotlib/", line 148, in autoscale_None
AttributeError: 'int' object has no attribute 'autoscale_None'

This part of the traceback is also a little puzzling; I'm not sure why self.norm is an int at this point.

I assume this a bug of some sort.

No, I think the problem is that you are passing a class instead of an instance of a class as the norm kwarg to hexbin. (It is not completely clear to me from the traceback, however--there is that strange single quote mark.) What kind of normalization are you trying to to? In other words, what are you trying to accomplish by specifying the norm kwarg?


Thanks for any ideas.
  os-x 10.5.6
  python 2.5.4 from macports
  matplotlib from macports

Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial.
Matplotlib-users mailing list