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

Emmanuelle Gouillart emmanuelle.gouillart@normalesup....
Mon Jun 1 06:20:21 CDT 2009

	Hi Robert,

>  histo = np.histogram(z.ravel(), bins=r_[Z.ravel(),2*n**2])

>    produeces the following error message:

>  Traceback (most recent call last):
>    File "/mnt/VBoxShare/eg.py", line 15, in <module>
>      histo = np.histogram(z.ravel(), bins=r_[Z.ravel(),2*n**2])
>  NameError: name 'r_' is not defined

	As Gaël said, it should be np.r_ instead of r_. It's just that I
executed my code into ipython -pylab that enables the interactive use of
matplotlib, but also loads a lot of pylab and numpy features into the
namespace. Sorry about the typo! You should try to execute the code
again. I used np.r_ to concatenate the array Z.ravel() with 2*n**2 in
order to add the upper_edge of the last bin for the histogram (note: if
you don't use a recent version of numpy, histogram may return an error).

	I read rapidly your code below, here are a few comments:
* I guess one of the problems might be that you're using two different
scales for the data and for your grid. pcolor(density_map) plots the
color levels corresponding to density_map on a y-scale (x-scale) between
0 and max_index_along_first_direction - 1 (that does not correspond to
the values of the data). That may explain why your data and your
density_map do not superpose. You should define X and Y coordinates of
the grid (e.g. using np.mgrid as in my example) and plot pcolor(X, Y,
* Rather than grid_x = [ block_x * i for i in range(11) ], use
np.linspace(0, block_x*11, 11, endpoint=False) or np.arange(0, block_x*11, block_x) 
* You can avoid easily your for loop using numpy.histogram2d that does
just what you want for putting your points inside the bins of the grid.
Try np.histogram2d(a, b, bins=11, range=[[0, xlim], [0, ylim]]) (check
the documentation first).

Hope this helps,

PS: actually this discussion should rather be on the numpy-discussion
list. I would advise you to suscribe to this list and -- if you have
further questions -- post your reply on the numpy-discussion list instead.

>    I'm very new to Scipy and have no idea what your intended to do there.

>    What I'm trying to do is the following:

>  from scipy import polyval, zeros
>  import pylab

>  a, b = fetch_data(...)

>  pylab.plot(a, b, "g.")  # scatter plot

>  # regression line
>  regression = regression_analysis(...)
>  xr = polyval([regression[0], regression[1]], b)
>  pylab.plot(b, xr, "r-")

>  pylab.gca().set_xlim([0,max(b)])
>  pylab.gca().set_ylim([0,max(a)])

>  # calculate grid (10x10)

>  xlim = pylab.gca().get_xlim()[1]
>  ylim = pylab.gca().get_ylim()[1]
>  block_x = int(xlim / 10.0 + 1)
>  block_y = int(ylim / 10.0 + 1)
>  grid_x = [ block_x * i for i in range(11) ]
>  grid_y = [ block_y * i for i in range(11) ]

>  density_map = zeros((10, 10))   # matrix for points per cell

>  inc = 1.0 / number_of_data_points

>  for i in range(10):
>      for j in range(10):
>          cell = [ grid_x[i], grid_x[i+1], grid_y[j], grid_y[j+1] ]
>          density_map[j][i] += points_in(cell) * inc

>  # plot the 'density map'
>  pylab.pcolor(density_map, cmap=pylab.get_cmap("hot"))

>  pylab.show()

>    This only creates the scatter plot and the regression line.

>    kind regards
>    robert

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

More information about the SciPy-user mailing list