Home > Back-end >  How to fill the logarithmic spiral with color
How to fill the logarithmic spiral with color

Time:05-21

I have written a snippet to draw a logarithmic spiral, now the curves drawing is done, but for the color filling part, I'm not familiar with how to fill it (part between the two curves, and part between the curve and outer circle border). How to finish the color filling part? Is fill_between more suitable here?

import matplotlib.pyplot as plt
b = 0.2
a = 2

theta = np.linspace(0, np.pi * 3.0, 1000, endpoint=True)


r = exp(b * theta) * a

theta0 = np.linspace(0, np.pi * 4.0, 1000, endpoint=True)
r0 = [r[-1]] * len(theta0)


theta2 = np.linspace(np.pi, np.pi * 4.0, 1000, endpoint=True)
r2 = exp(b * theta) * a

ax = plt.subplot(111, projection='polar')

ax.set_rmax(r[-1])
ax.fill(theta0, r0, c='blue')
ax.plot(theta, r)

ax.plot(theta2, r2)
ax.fill(theta2, r2, 'red')


theta3 = np.linspace(0, np.pi * 2.0, 1000, endpoint=True)
r3 = [a] * len(theta3)

plt.box(on=None)
ax.fill(theta3, r3, c='black')

ax.grid(False)
plt.show()

What I have got now: enter image description here

The expected result: enter image description here

CodePudding user response:

One way is to create a close line for the fill command, putting together the different pieces:

import matplotlib.pyplot as plt
import numpy as np

b = 0.2
a = 2

theta1 = np.linspace(0, np.pi * 3.0, 1000, endpoint=True)
r1 = np.exp(b * theta1) * a
theta2 = np.linspace(np.pi, np.pi * 4.0, 1000, endpoint=True)
r2 = np.exp(b * theta1) * a
theta3 = np.linspace(np.pi, 0, 1000)
r3 = r1[-1] * np.ones_like(theta3)
theta4 = np.linspace(np.pi, 2 * np.pi, 1000)
r4 = a * np.ones_like(theta4)
theta5 = np.linspace(np.pi, 2*np.pi, 1000)
r5 = r1[-1] * np.ones_like(theta5)
theta6 = np.linspace(0, np.pi, 1000)
r6 = a * np.ones_like(theta6)

# put together the different pieces
theta_final_red = np.concatenate([theta1, theta3, np.flip(theta2), theta4])
radius_red = np.concatenate([r1, r3, np.flip(r2), r4])
theta_final_blue = np.concatenate([theta1, theta5, np.flip(theta2), theta6])
radius_blue = np.concatenate([r1, r5, np.flip(r2), r6])

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.set_rmax(r1[-1])

ax.fill(theta_final_red, radius_red, "r")
ax.fill(theta_final_blue, radius_blue, "b")
ax.plot(theta1, r1)
ax.plot(theta2, r2)

# black inner circle
theta3 = np.linspace(0, np.pi * 2.0, 1000, endpoint=True)
r3 = [a] * len(theta3)
ax.fill(theta3, r3, c='black')
ax.axis(False)
ax.grid(False)
plt.show()

enter image description here

  • Related