# [SciPy-user] How to use pcolor and scatter plot in one image?

Emmanuelle Gouillart emmanuelle.gouillart@normalesup....
Sun May 31 07:41:51 CDT 2009

```Oops sorry, there was a bug in my code as I interchanged x and y (this
proves once again that tests should always be based on non-symmetric data!)
Here is the corrected version

Emmanuelle

import numpy as np
import pylab as pl

N = 1000
n = 10
np.random.seed(3)
x, y = np.random.randn(2, N)/10 +0.5
y -= 0.1
X, Y = np.mgrid[0:1:n*1j, 0:1:n*1j]

xfloor = X[:,0][np.floor(n*x).astype(int)]
yfloor = Y[0][np.floor(n*y).astype(int)]
z = yfloor + n*xfloor
Z = Y + n*X
histo = np.histogram(z.ravel(), bins=r_[Z.ravel(),2*n**2])

pl.pcolor(X-1./(2*n), Y-1./(2*n), histo[0].reshape((n,n)))
pl.scatter(x, y)
show()

On Sun, May 31, 2009 at 02:10:19PM +0200, Emmanuelle Gouillart wrote:

> ***
> import numpy as np
> import pylab as pl

> N = 1000
> n = 10
> np.random.seed(3)#use always the same seed
> x, y = np.random.randn(2, N)/10 +0.5
> X, Y = np.mgrid[0:1:n*1j, 0:1:n*1j]

> xfloor = X[:,0][np.floor(n*x).astype(int)]
> yfloor = Y[0][np.floor(n*y).astype(int)]
> z = xfloor + n*yfloor
> Z = X + n*Y
> histo = np.histogram(z.ravel(), bins=r_[Z.T.ravel(),2*n**2])

> pl.pcolor(X-1./(2*n), Y-1./(2*n), histo[0].reshape((n,n))) #shifted to
> # have centered bins
> pl.scatter(x, y)
> pl.show()

> On Sun, May 31, 2009 at 12:59:23PM +0200, wierob wrote:
> > Hi,

> > how can I use pcolor and a scatter plot in one image?

> > I have a scatter plot where a lot of data points are so close to each
> > other that they are drawn as (almost) one point in the scatter plot. So
> > I'm trying to visualize which area of the scatter plot contains the most
> > data points. Using pcolor I can draw a gird where each cell visualizes
> > the relative number of data points by a different color.

> > If I try to draw the scatter plot and the grid in the same image, only
> > the scatter plot will be drawn. Regardless of the invocation order of
> > plot and pcolor.

> > ...
> > plot(...)
> > pcolor(...)
> > show()

> > ...
> > pcolor(...)
> > plot(...)
> > show()

> > Both return only the scatter plot.

> > I'm new to Scipy. What am I doing wrong?

> > kind regards
> > robert
> > _______________________________________________
> > SciPy-user mailing list
> > SciPy-user@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
```