4

I am trying to plot the function of two variables using matplotlib. The function is stored in three 1d arrays X, Y and F corresponding to x-coordinate, y-coordinate and the value of the function. Is it possible to plot these data as a contour plot? Before I saw the solution with griddata(), but I would like to avoid interpolating since x and y coordinates are already well defined.

freude
  • 3,632
  • 3
  • 32
  • 51
  • Possible duplicate: http://stackoverflow.com/questions/14120222/matplotlib-imshow-with-irregular-spaced-data-points – Hooked Apr 18 '13 at 13:46
  • There the function griddata() has been used. I can build any distribution of coordinates in arrays X and Y including the regular uniform one, so I would like to avoid additional interpolating. Therefore, that link does not help. – freude Apr 18 '13 at 13:50

1 Answers1

5

Take a look at the contour demo of the matplotlib docs. Since you say you can calculate your function F exactly at any given point:

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
F = your_function(X.ravel(), Y.ravel())
CS = plt.contour(X, Y, F.reshape(X.shape))
plt.clabel(CS, inline=1, fontsize=10)
Jaime
  • 65,696
  • 17
  • 124
  • 159
  • What is the difference between Z and F? In my case, F is the one-dimensional array – freude Apr 18 '13 at 15:39
  • @freude `Z` was a typo, should be `F`. You have to reshape your data to a 2D grid, see my edit on how to go about that. – Jaime Apr 18 '13 at 16:04