I would like to draw the curve a generic cubic function using matplotlib. I want to draw curves that are defined by functions such as: x^3 + y^3 + y^2 + 2xy^2 = 0. Is this possible to do?
2 Answers
One obvious way to do this is to found the (x,y) pairs satisfy the relationship, by numerically solving the equation.
from scipy import optimize
f=lambda x, y: (x**3+y**3+y**2+2*x*y*y-0)**2
y_range=linspace(-1, 1, 100)
x_range=[optimize.fmin(f,0,args=(y,), disp=0) for y in y_range]
xr=linspace(-1,1)
yr=linspace(-1,1)
X, Y=meshgrid(xr, yr)
Z=f(X, Y)
plt.plot(x_range, y_range, 'k')
plt.contourf(xr, yr, Z, levels=linspace(0,0.001,51), alpha=0.5)
plt.colorbar()

The black line is what you want. The contour is just to show how the function behaves around 0. optimize.fmin() is not the most efficient solver here, just keep it simple.
When the absolute values of x or y are large, you are essentially plotting x+0.4496y=0 and you don't need to do all these above.