I want to write a program that takes in strings representing functions, such as "x^2+y^2+z^2=30" and plot it in matplotlib. I want my program to be able to handle general cases. Previously for plotting general 2d implicit functions in matplotlib I was using spb.plot, which handled implicit functions very nicely. Unfortunately I can't find anything that would work for handling a wide variety of 3d functions.
import matplotlib.pyplot as plt
import spb
import numpy as np
def queryMatplotlib3dEq(qVal):
fig = plt.figure()
lhs, rhs = qVal.split("=")
fig = plt.figure()
ax = plt.axes(projection="3d")
def f(x, y):
if(lhs == "z"):
return eval(rhs)
else:
return eval(lhs)
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
if(lhs =='z' or rhs == 'z'):
Z = f(X, Y)
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()
#Can do this
qVal = "z=x**2+y**2"
#Cannot do this
qVal = "x^2+y^2+z^2=30"
queryMatplotlib3dEq(qVal)