Hemfri
Hemfri

Reputation: 325

How to draw circles on the perimeter of a circle?

I'm trying to plot something like this:

Circles

I don't know how to find the center of smaller circles in for loops. First, I've tried to plot it with smaller number of circles(for example 2) but I don't know why the smaller circles are semi-circles??

My try:

import numpy as np
import matplotlib.pyplot as plt

r = 2, h = 1, k = 1

axlim = r + np.max((abs(h),np.max(abs(k))))
x = np.linspace(-axlim, axlim, 100)


X,Y = np.meshgrid(x,x)

F = (X-h)**2 + (Y-k)**2 - r**2
plt.contour(X,Y,F,0)


F1 = (X-(h+r))**2 + (Y-k)**2 - (r/3)**2
plt.contour(X,Y,F1,0)

F2 = (X-h)**2 + (Y-(k+r))**2 - (r/3)**2
plt.contour(X,Y,F2,0)

plt.gca().set_aspect('equal')
plt.axis([-4*r, 4*r, -4*r,4*r])
# plt.axis('off')
plt.show()

The output: enter image description here

Upvotes: 2

Views: 829

Answers (1)

JohanC
JohanC

Reputation: 80329

Sine, cosine and an angle evenly divided over the range 0, 2picould be used:

import numpy as np
import matplotlib.pyplot as plt

num_circ = 7
rad_large = 7
rad_small = 6
thetas = np.linspace(0, 2 * np.pi, num_circ, endpoint=False)

fig, ax = plt.subplots()
ax.add_patch(plt.Circle((0, 0), rad_large, fc='none', ec='navy'))
for theta in thetas:
    ax.add_patch(plt.Circle((rad_large * np.cos(theta), rad_large * np.sin(theta),), rad_small, fc='none', ec='crimson'))
ax.autoscale_view() # calculate the limits for the x and y axis
ax.set_aspect('equal') # show circles as circles
plt.show()

example plot

Upvotes: 3

Related Questions