# Add hist2d plotting function #805

Closed
wants to merge 5 commits into
from

## Conversation

Projects
None yet
2 participants
Contributor

### piti118 commented Mar 28, 2012

 This seems to be missing from matplotlib. While fancy hexbin exists, a simple rectangular bin histogram isn't there. This is very useful for viewing correlation between variables. http://stackoverflow.com/questions/2030970/python-matplotlib-rectangular-binning (4.7k view so it's not only me googling for this) and also #668

### piti118 added some commits Mar 28, 2012

``` add hist2d function (just a wrapper of np.histogram2d and imshow ) ```
``` 7b97eb7 ```
``` add missing sci for hist2d boilerplate ```
``` b0e0a7b ```
``` add simple example ```
``` 704fcca ```
Member

### dmcdougall commented Mar 30, 2012

 Haven't looked at the code in depth yet, but I've tried the example; it works and it looks nice! Good job, though. I think this is a nice addition!

### dmcdougall commented on the diff Mar 30, 2012

lib/matplotlib/axes.py
 + + # xrange becomes range after 2to3 + bin_range = range + range = __builtins__["range"] + h,xedges,yedges = np.histogram2d(x, y, bins=bins, range=bin_range, normed=False, weights=weights) + + if 'origin' not in kwargs: kwargs['origin']='lower' + if 'extent' not in kwargs: kwargs['extent']=[xedges[0], xedges[-1], yedges[0], yedges[-1]] + if 'interpolation' not in kwargs: kwargs['interpolation']='nearest' + if 'aspect' not in kwargs: kwargs['aspect']='auto' + if cmin is not None: h[hcmax]=None + + im = self.imshow(h.T,**kwargs) + + return h,xedges,yedges,im

#### dmcdougall Mar 30, 2012

Member

I'm not sure returning this big tuple is the best thing to do... I'll try to think of something neater either tomorrow or over the weekend.

#### piti118 Mar 30, 2012

Contributor

My rationale was to make it semi-consistent with hist return value.

#### piti118 Mar 30, 2012

Contributor

I think returning hist is a good idea though incase people wanna do something like drawing a number over the color patches.

#### dmcdougall Mar 30, 2012

Member

Good point. Though, another option would be to return a `PolyCollection` instance instead of wrapping around `imshow`, just like `hexbin` does.

#### piti118 Mar 30, 2012

Contributor

I know. This is why I didn't it rectbin XD. I'm not very familiar with manipulating matplotlib primitive types though. Yet, I agree that returning PolyCollection or array of square(or whatever) is more appropriate and closer to the patches return as the last element in hist.

### piti118 added some commits Apr 3, 2012

``` make hist(....,histtype='step') return value consistent with hist(...… ```
```….,histtype='bar')

It now returns either poly object for single histogram and list of polyobject for multiple histograms```
``` f655103 ```
``` making a new branch for a separate pull request ```
```Revert "make hist(....,histtype='step') return value consistent with hist(....,histtype='bar')"

This reverts commit f655103.```
``` 457ac69 ```
Contributor

### piti118 commented Apr 13, 2012

 close to make a new clean PR

### This was referenced Apr 13, 2012

Merged

Closed

#### squarebin or rectbin function #668

to join this conversation on GitHub. Already have an account? Sign in to comment