如何绘制一个扰动的圆?

时间:2016-11-25 14:09:51

标签: python numpy matplotlib

如何绘制一个Perturbed Circle?

import matplotlib.pyplot as plt

import numpy as np

e=0.3

ylist = np.linspace(0, 2*np.pi, 20)

R=1+e*np.sin(2*ylist)


circle1 = plt.Circle((0, 0),R)

fig, ax = plt.subplots()

ax.add_artist(circle1)

plt.axis([-3, 3, -3, 3])

1 个答案:

答案 0 :(得分:1)

您无法将列表设置为Circle的半径。 可能还有其他方法来吸引"扰乱"但是我发现使用plt.contour()绘制参数曲线非常直观。

半径为R的完美圆的参数曲线为

0 = x**2 + y**2 - R

虽然在这种情况下是

0 = x**2 + y**2 - 1 -e*np.sin(2*np.arctan(y/x)) =: f

因此,我们可以绘制f(x,y),并在调用contour(X,Y,Z, levels)时将levels设置为0

import matplotlib.pyplot as plt
import numpy as np

e=0.3

x = np.linspace(-3.0, 3.0, 101)
X, Y = np.meshgrid(x, x)
f = lambda x,y: x**2 + y**2 - 1 -e*np.sin(2*np.arctan(y/x))

fig, ax = plt.subplots(figsize=(3,3))
ax.contour(X,Y, f(X, Y), 0)

plt.axis([-2, 2, -2, 2])
plt.gca().set_aspect("equal")
plt.axis('off')
plt.savefig(__file__+".png")
plt.show()

enter image description here