user5875780
user5875780

Reputation:

How to draw a perturbed circle?

How do I draw a 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])

Upvotes: 0

Views: 279

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339470

You cannot set a list as radius to a Circle. There might be other methods to draw a "disturbed" circle, but I find it very intuitive to draw a parametric curve using plt.contour().

The parametric curve for a perfect circle with radius R would be

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

while in this case it's

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

So we can plot f(x,y) and in the call to contour(X,Y,Z, levels) we set levels to 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

Upvotes: 1

Related Questions