Yash
Yash

Reputation: 699

Plotting function that returns complex values using pyplot

I found this image on Facebook and wanted to plot it, as I thought it will be a good opportunity to learn something new.

 love formula
(source: akamaihd.net)

The 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()

The Output:

IMG

I am unable to obtain the plot as given in the figure. In order to plot, I reordered the formula into y = f(x) form and translated it into the code above.

Upvotes: 3

Views: 785

Answers (1)

unutbu
unutbu

Reputation: 879083

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()

enter image description here


You can play with the aspect-ratio to make the curve a bit more heart-like:

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()

enter image description here

Upvotes: 7

Related Questions