使用pyplot绘制返回复数值的函数

3

我在Facebook上发现了这张图片并想要绘制它,因为我认为这将是学习新东西的好机会。

爱的公式
(来源:akamaihd.net)

代码:

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

输出结果:

IMG

我无法得到如图所示的绘图。 为了绘制,我将公式重新排序为 y = f(x) 的形式,并翻译成上面的代码。

1个回答

7
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


您可以使用宽高比来使曲线更像心形:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接