您可以通过将 2 pi 分成 n+1 相等的段来创建一个 ngon。您可以选择为第一个点添加一个角度:
import numpy as np
import matplotlib.pyplot as plt
def plot_ngon(n, rad, start_angle=0, **kwargs):
ax.plot(np.linspace(start_angle, start_angle + 2 * np.pi, (n + 1)), np.full(n + 1, rad), **kwargs)
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(np.linspace(0, 2 * np.pi, 200), np.ones(200) * 5, color='r', linestyle='-')
ax.plot(np.linspace(0, 2 * np.pi, 200), np.ones(200) * 6, color='b', linestyle='-')
ax.plot(np.linspace(0, 2 * np.pi, 200), np.ones(200) * 4, color='g', linestyle='-')
plot_ngon(n=3, rad=4, color='g', linestyle='-')
plot_ngon(n=3, rad=3, start_angle=np.pi, color='g', linestyle='-')
plt.show()
中心图和右图由以下方式生成:
plot_ngon(n=200, rad=6, color='red', linestyle='-')
for rad in np.linspace(6, 1, 20):
plot_ngon(n=3, rad=rad, start_angle=rad/2, color=plt.cm.inferno((rad-1) / 6))
和
for rad in np.linspace(6, 0, 50):
plot_ngon(n=4, rad=rad, start_angle=rad, color=plt.cm.winter(rad / 6))
PS:要绘制等式r ≤ sin(a / b * xs),可以使用以下方法。请注意,xs 应该有足够的位置,以便 a / b * xs 填充 0 到 2 pi 之间的整个范围。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
xs = np.linspace(0, 12 * np.pi, 500)
a = 7
b = 10
rs = np.abs(np.sin(a / b * xs)) # or rs = np.clip(np.sin(a / b * xs), 0, 1)
ax.plot(xs, rs, color='fuchsia')
ax.fill_between(xs, 0, rs, color='fuchsia', alpha=0.3)
plt.show()