0

I have X coordinates ,Y coordinates and Zcoordinates, each stored in an array with n x 1 . (n rows,1 column) .The contour plot in Matplotlib allows to plot only if "*X and Y must both be 2-D with the same shape as Z, or they must both be 1-D such that len(X) is the number of columns in Z and len(Y) is the number of rows in Z.*"

How can I solve this issue?
Also the corresponding x,y,z values should be accessible.
Mixing of data points will cause error in my plot.

Destrif
  • 2,104
  • 1
  • 14
  • 22
  • How would you expect the contourplot to look like with 1D z-coordinates(assuming z is what you plot on the x-y plane)? Are you sure you are not looking for a scatterplot or 3d-line plot? – M.T Jul 08 '16 at 08:02
  • You could improve this question by providing a minimum working example of the code, which reproduces the error you're getting. – Aleksander Lidtke Jul 08 '16 at 08:45

2 Answers2

0

The way I know is to evaluate z over a grid, composed of x and y:

X, Y = np.meshgrid(x, y)
plt.plot(X, Y, z)
z = <evaluate over X, Y>
plt.contour(X, Y, z)
plt.show()

This explains it a bit better

alexpeits
  • 847
  • 8
  • 18
  • 1
    you are assuming that z is defined as a function that can be evaluated on the new grid. It might be a list of spot height measurements of a mountain, so this approach wont work. – VBB Jul 08 '16 at 08:18
0

Contour expects gridded data. This matplotlib tutorial explains it perfectly: http://matplotlib.org/examples/pylab_examples/griddata_demo.html. Also see Make contour of scatter.

Community
  • 1
  • 1
VBB
  • 1,305
  • 7
  • 17
  • 2
    Although quite unlikely in this case, it is a good habit to *Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.* [answer guidelines](http://stackoverflow.com/help/how-to-answer) – M.T Jul 08 '16 at 08:26
  • Thanks - will keep that in mind :) For this particular one, as you've already pointed out, it would be unlikely here as the sites are stackoverflow itself and matplotlib. – VBB Jul 08 '16 at 09:05