Contour Plot, TypeError: Length Of Y Must Be Number Of Rows In Z
I have created a function with two arguments, func(x,y). I would now like to plot this as a 2D contour plot, first as func(x,y) vs. x, and then func(x,y) vs. y. I set my numpy arr
Solution 1:
From the docs for contour
: "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." (This is the version for 1D X and Y.)
Your basic problem here is that Z
needs to be rectangular, so probably 20x20 in your case. That is, think of a contour plot as putting levels on something like an image.
As best I can figure it out, here's a working version that's along the lines of what you want:
import pylab as pb
import numpy as np
import matplotlib.pyplot as plt
def f(x, y):
return np.log(x**2 + y**2)
x = np.logspace( np.log10(5e4), np.log10(8e4), num=20)
y = np.logspace(np.log10(5e4), np.log10(9e4), num=20)
X, Y = np.meshgrid(x, y)
z = f(X, Y)
print x
print y
print min(z.flat), max(z.flat), min(x), max(x), min(y), max(y)
plt.figure()
CS = plt.contour(x, y, z)
plt.clabel(CS, inline=1, fontsize=10)
pb.show()
I think the key that you're not using meshgrid
(although there are other means of getting this too).
Post a Comment for "Contour Plot, TypeError: Length Of Y Must Be Number Of Rows In Z"