Plot function that returns complex values with pyplot
I found this image on Facebook and wanted to draw it because I thought it would be a good opportunity to learn something new.
(source: akamaihd.net )
Code:
import numpy as np
import scipy.special
import pylab
x = np.linspace( -1.0, 1.0, 200 )
y = np.sqrt( 1-np.square(np.complex64(x) ) ) + np.complex64( scipy.special.cbrt( np.square( x ) ) )
pylab.plot( np.complex64( x ), y )
pylab.show()
Output:
I cannot get the plot as shown in the picture. To plot the graph, I rearranged the formula in the form y = f(x)
and translated it into the code above.
+3
source to share
1 answer
import matplotlib.pyplot as plt
import numpy as np
y, x = np.ogrid[-1:2:100j, -1:1:100j]
plt.contour(x.ravel(), y.ravel(), x**2 + (y-((x**2)**(1.0/3)))**2, [1])
plt.axis('equal')
plt.show()
You can play with the aspect ratio to make the curve a little more hearty:
fig = plt.figure() ax = fig.add_subplot(1, 1, 1) y, x = np.ogrid[-1.5:2:1000j, -2:2:1000j] plt.contour(x.ravel(), y.ravel(), x**2 + (y-((x**2)**(1.0/3)))**2, [1]) ax.set_aspect(0.75) plt.show()
+7
source to share