I am trying to use the content of a python function to make a contour plot. For example I try:
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
# Building my equation from a series of matrix operation
def func_M(X, Y):
z_mat = np.array([[X**2, Y],
[Y, X]])
z_other = np.array([[X],
[Y]])
z_matInv = (np.linalg.inv(z_mat))
z_final=np.dot(z_matInv,z_other)
return z_final[0][0]
# print out the return of the function evaluated at X = .1.5, and Y = 2.
print func_M(1.5,2.)
# Building and x and y grid to plot:
x = np.arange(-5, 5, 0.01)
y = np.arange(-5, 5, 0.01)
xx, yy = np.meshgrid(x, y, sparse=True)
# Making the z component and contour plotting function:
#z_plot_M = func_M(xx,yy)
#h = plt.contourf(x,y,z_plot_M)
#plt.show()
So, the print function prints out 2.8, which is the first element of the final matrix evaluated at x = 1.0 and y =2.0.
However, if I try to evaluate this to plot as a countour (the commented bit) it doesn't work.
Here's the simple code I was was using as the basis:
x = np.arange(-5, 5, 0.01)
y = np.arange(-5, 5, 0.01)
xx, yy = np.meshgrid(x, y, sparse=True)
def func_eq(X, Y):
z = np.sin(X**2) + np.sin(Y**2)
return z
z_plot = func_eq(xx,yy)
h = plt.contourf(x,y,z_plot)
plt.show()
Is there a reason why this works but when I have matrices instead of a simple equation it doesn't? and is there an easy fix it for the first case?