python - Contour plot, TypeError: Length of y must be number of rows in z -
i have created function 2 arguments, func(x,y)
. plot 2d contour plot, first func(x,y)
vs. x
, , func(x,y)
vs. y
.
i set numpy array x-values 20 values 5e4
8e4
, , y-values 20 values 1e10
1e12
. function func(x,y)
takes these 2 arrays arguments.
so, set plot follows:
import matplotlib import matplotlib.pyplot plt import numpy np import matplotlib.cm cm import matplotlib.mlab mlab matplotlib.rcparams['xtick.direction'] = 'out' matplotlib.rcparams['ytick.direction'] = 'out' x = np.logspace( 5e4, 8e4, num=20) y = np.logspace(1e10, 1e12, num=20) z = np.asarray([ func(x, y), x ]) plt.figure() cs = plt.contour(x, y, z) plt.clabel(cs, inline=1, fontsize=10)
the shapes numpy arrays are:
print x.shape print y.shape print z.shape
which outputs
(20,) (20,) (2, 20)
my error
typeerror: length of y must number of rows in z.
why be? dimensions correct.
from the docs contour
: "x , y must both 2-d same shape z, or must both 1-d such len(x) number of columns in z , len(y) number of rows in z." (this version 1d x , y.)
your basic problem here z
needs rectangular, 20x20 in case. is, think of contour plot putting levels on image.
as best can figure out, here's working version that's along lines of want:
import pylab pb import numpy np import matplotlib.pyplot 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 key you're not using meshgrid
(although there other means of getting too).
Comments
Post a Comment