如何用颜色填充对数螺线
How to fill the logarithmic spiral with color
我写了一段画对数螺线的代码,现在曲线画好了,但是对于颜色填充部分,我不熟悉如何填充它(两条曲线之间的部分,和之间的部分曲线和外圆边界)。填色部分如何完成? fill_between
这里更合适吗?
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()
我现在得到的是:
一种方法是为填充命令创建一条闭合线,将不同的部分放在一起:
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()
我写了一段画对数螺线的代码,现在曲线画好了,但是对于颜色填充部分,我不熟悉如何填充它(两条曲线之间的部分,和之间的部分曲线和外圆边界)。填色部分如何完成? fill_between
这里更合适吗?
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()
我现在得到的是:
一种方法是为填充命令创建一条闭合线,将不同的部分放在一起:
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()