freude
freude

Reputation: 3832

2D plot using 1D arrays without griddata()

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.

Upvotes: 4

Views: 2848

Answers (1)

Jaime
Jaime

Reputation: 67427

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)

Upvotes: 5

Related Questions