我已經寫了一個片段來繪制對數螺旋,現在曲線繪制完成了,但是對于顏色填充部分,我不熟悉如何填充它(兩條曲線之間的部分,以及曲線和外圓之間的部分邊界)。如何完成顏色填充部分?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()
我現在得到的:

預期結果:

uj5u.com熱心網友回復:
一種方法是為填充命令創建一條閉合線,將不同的部分放在一起:
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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/478897.html
標籤:Python matplotlib 朱庇特
