[Numpy-discussion] Pull Request regarding meshgrid

Paul Reiter reiter.paul at gmail.com
Sun Sep 4 09:39:32 EDT 2016


https://github.com/numpy/numpy/pull/7984

Hi everybody,
I created my first pull request for numpy and as mentioned in the numpy
development workflow documentation I hereby post a link to it and a short
description to the mailing list.
Please take a look.


I didn't find a good way to create a contour plot of data of the form:
[(x1, y1, f(x1, y1)), (x2, y2, f(x2, y2)), ..., (xn, yn, f(xn, yn))].
In order to do a contour plot, one has to bring the data into the meshgrid
format.
One possibility would be complicated sorting and reshaping of the data, but
this is not easily possible especially if values are missing (not all
combinations of (x, y) contained in data).
Another way, which is used in all tutorials about contour plotting, is to
create the meshgrid beforehand and than apply the function to the meshgrid
matrices:

    x = np.linspace(-3, 3, n)
    y = np.linspace(-3, 3, n)
    X, Y = np.meshgrid(x, y)
    Z = f(X, Y)
    plt.contourplot(X, Y, Z)

But if one does not have the function but only the data, this is also no
option.
My function essentially creates a dictionary {(x1, y1): f(x1, y1), (x2,
y2): f(x2, y2), ..., (xn, yn): f(xn, yn)} with the coordinate tuples as
keys and function values as values. Then it creates a meshgrid from all
unique x and y coordinates (X and Y). The dictionary is then used to create
the matrix Z, filling in np.nan for all missing values. This allows to do
the following, with x, y and z being the x, y coordinates and z being the
according function value:

    plt.contourplot(*meshgridify(x, y, f=z))

Maybe there is a simpler solution, but I didn't find one.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160904/d0b910ec/attachment.html>


More information about the NumPy-Discussion mailing list