Contour plot, TypeError: Length of y must be number of rows in z

Asked
Active3 hr before
Viewed126 times

3 Answers

numbercontour
90%

Where X and Y are 2D arrays of the x and y points, and Z is a 2D array of points that determines the color of the areas on the 2D plot,,Where X and Y are 2D arrays of the x and y points, and Z is a 2D array of points that determines the "height" of the contour, which is represented by color in a 2D plot

Example_snippet/controller/utility/_number.js/ ax.contour(X, Y, Z) . . .
ax.contour(X, Y, Z)
Step 2 continued with import numpy as np import matp. . .
import numpy as np
import matplotlib.pyplot as plt
#
if using a Jupyter notebook, include:
   %
   matplotlib inline
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
fig, ax = plt.subplots(figsize = (6, 6))
ax.contour(X, Y, Z)
plt.show()
Step 3 continued with ax.contourf(X, Y, Z) . . .
ax.contourf(X, Y, Z)
Step 4 continued with import numpy as np import matp. . .
import numpy as np
import matplotlib.pyplot as plt
#
if using a Jupyter notebook, include:
   %
   matplotlib inline
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
fig, ax = plt.subplots(figsize = (6, 6))
ax.contourf(X, Y, Z)
plt.show()
Step 5 continued with cf = ax.contourf(X,Y,Z) fig.co. . .
cf = ax.contourf(X, Y, Z)
fig.colorbar(cf, ax = ax)
Step 6 continued with import numpy as np import matp. . .
import numpy as np
import matplotlib.pyplot as plt
#
if using a Jupyter notebook, include:
   %
   matplotlib inline
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
fig, ax = plt.subplots(figsize = (6, 6))
ax.set_aspect('equal')
cf = ax.contourf(X, Y, Z)
fig.colorbar(cf, ax = ax)
plt.show()
Step 7 continued with mycmap = plt.get_cmap('gist_ea. . .
mycmap = plt.get_cmap('gist_earth')
ax.contourf(X, Y, Z, cmap = mycmap)
Step 8 continued with import matplotlib.pyplot as pl. . .
import matplotlib.pyplot as plt
import numpy as np
#
if using a Jupyter notebook, include:
   %
   matplotlib inline

x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)

X, Y = np.meshgrid(x, y)

Z = np.sin(X) * np.cos(Y)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (12, 6))

mycmap1 = plt.get_cmap('gist_earth')
ax1.set_aspect('equal')
ax1.set_title('Colormap: gist_earth')
cf1 = ax1.contourf(X, Y, Z, cmap = mycmap1)

fig.colorbar(cf1, ax = ax1)

mycmap2 = plt.get_cmap('gnuplot2')
ax2.set_aspect('equal')
ax2.set_title('Colormap: gnuplot2')
cf2 = ax2.contourf(X, Y, Z, cmap = mycmap2)

fig.colorbar(cf2, ax = ax2)

plt.show()
load more v
88%

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

Example_snippet/controller/utility/_number.js/ import matplotlib import matpl. . .
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as 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)
Step 2 continued with print x.shape print y.shape pr. . .
print x.shape
print y.shape
print z.shape
Step 3 continued with (20,) (20,) (2, 20) . . .
(20, )
(20, )
(2, 20)
Step 4 continued with TypeError: Length of y must be. . .
TypeError: Length of y must be number of rows in z.
load more v
72%

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

Example_snippet/controller/utility/_contour.js/ import pylab as pb import . . .
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()