# [Numpy-discussion] Convert data into rectangular grid

David Huard david.huard@gmail....
Tue Sep 29 23:08:20 CDT 2009

```On Mon, Sep 28, 2009 at 8:45 PM, jah <jah.mailinglist@gmail.com> wrote:

> On Mon, Sep 28, 2009 at 4:48 PM, <josef.pktd@gmail.com> wrote:
>
>> On Mon, Sep 28, 2009 at 7:19 PM, jah <jah.mailinglist@gmail.com> wrote:
>> > Hi,
>> >
>> > Suppose I have a set of x,y,c data (something useful for
>> > matplotlib.pyplot.plot() ).  Generally, this data is not rectangular at
>> > all.  Does there exist a numpy function (or set of functions) which will
>> > take this data and construct the smallest two-dimensional arrays X,Y,C (
>> > suitable for matplotlib.pyplot.contour() ).
>> >
>> > Essentially, I want to pass in the data and a grid step size in the x-
>> and
>> > y-directions.  The function would average the c-values for all points
>> which
>> > land in any particular square.  Optionally, I'd like to be able to
>> specify a
>> > value to use when there are no points in x,y which are in the square.
>> >
>> > Hope this makes sense.
>>
>> If I understand correctly  numpy.histogram2d(x, y, ..., weights=c) might
>> do
>> what you want.
>>
>> There was a recent thread on its usage.
>>
>
> It is very close, but it normed=True, will first normalize the weights
> (undesirably) and then it will normalize the normalized weights by dividing
> by the cell area.  Instead, what I want is the cell value to be the average
> off all the points that were placed in the cell.  This seems like a common
> use case, so I'm guessing this functionality is present already.  So if 3
> points with weights [10,20,30] were placed in cell (i,j), then the cell
> should have value 20 (the arithmetic mean of the points placed in the cell).
>
>
Would this work for you ?

>>> s = histogram2d(x,y,weights=c)  # Not normalized, so you get the sum of
the weights
>>> n = histogram2d(x,y) # Now you have the number of elements in each bin
>>> mean = s/n

David

> Here is the desired use case:  I have a set of x,y,c values that I could
> pass into matplotlib's scatter() or hexbin().   I'd like to take this same
> set of points and transform them so that I can pass them into matplotlib's
> contour() function.  Perhaps matplotlib has a function which does this.
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090930/acda6b55/attachment.html
```