gpt4 book ai didi

python - 在 matplotlib 中使用 fill 或 fill_between 绘制 donut

转载 作者:太空狗 更新时间:2023-10-30 01:44:44 28 4
gpt4 key购买 nike

我想画一个 donut ,我的脚本是

import numpy as np
import matplotlib.pyplot as plt
pi,sin,cos = np.pi,np.sin,np.cos

r1 = 1
r2 = 2

theta = np.linspace(0,2*pi,36)

x1 = r1*cos(theta)
y1 = r1*sin(theta)

x2 = r2*cos(theta)
y2 = r2*sin(theta)

如何获得带有红色填充区域的 donut ?

最佳答案

可以遍历封闭曲线区域的边界,使用fill方法填充封闭区域内的区域:

import numpy as np
import matplotlib.pyplot as plt

n, radii = 50, [.7, .95]
theta = np.linspace(0, 2*np.pi, n, endpoint=True)
xs = np.outer(radii, np.cos(theta))
ys = np.outer(radii, np.sin(theta))

# in order to have a closed area, the circles
# should be traversed in opposite directions
xs[1,:] = xs[1,::-1]
ys[1,:] = ys[1,::-1]

ax = plt.subplot(111, aspect='equal')
ax.fill(np.ravel(xs), np.ravel(ys), edgecolor='#348ABD')

plt.show()

circle

这可以很容易地应用于任何形状,例如,圆内或圆外的五边形:

pentagon

关于python - 在 matplotlib 中使用 fill 或 fill_between 绘制 donut ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22789356/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com