## matplotlib-users

 [Matplotlib-users] Plotting irregular 2d data From: Nikolaus Rath - 2010-06-08 21:56:04 ```Hi, I have a 1d array Z that unfortunately contains information about a two dimensional surface. The mapping is nontrivial (i.e. Z is not just a sequence of column or row information that I could reshape) but homogenous (i.e. there is a rectangular grid and Z contains data for each vertex). The x and y coordinates for each datapoint in Z are contained in separate arrays X and Y. A simple pyplot.contour(X, Y, Z) refuses to plot the data because Z is not 2d ("TypeError: Input z must be a 2D array"). What's the best way to plot this data? Is there an easy way to reshuffle the elements of Z so that they are in either row-column or column-row order? Best, -Nikolaus -- »Time flies like an arrow, fruit flies like a Banana.« PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C ```
 Re: [Matplotlib-users] Plotting irregular 2d data From: Friedrich Romstedt - 2010-06-08 23:09:38 ```Z = numpy.zeros((y_shape, x_shape)) x = your_flat_indices_in_x y = your_flat_indices_in_y z = your_flat_z_data If you have only coordinates, then try to figure out the indices in some way. Then do: Z[zip(y, x)] = z and figure out the coordinates that correspond to the mesh meant by Z. It's "fancy indexing". >>> Z = numpy.zeros((2, 2)) >>> x = numpy.asarray([0, 1]) >>> y = numpy.asarray([0, 0]) >>> z = numpy.asarray([10, 42]) >>> Z[zip(y, x)] = z >>> Z array([[ 10., 42.], [ 0., 0.]]) hth and is appropriate, Friedrich ```