绘制带填充的甜甜圈图
我想画一个甜甜圈图,我的代码是
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)
怎么才能让这个甜甜圈的填充区域变成红色呢?
4 个回答
0
tom10给出的答案是十 ;) 但如果你想定义圆形(甜甜圈)的原点,其实很简单,只需要在x和y的位置上加上xI、yI、yO、-yO和-yI就可以了:
ax.fill_between(x+pos[0], yI+pos[1], yO+pos[1], color=color)
ax.fill_between(x+pos[0], -yO+pos[1], -yI+pos[1], color=color)
如下所示:
import numpy as np
import matplotlib.pyplot as plt
import math
def plot_circle_donut(pos, inner, outer, color):
"""
REF: https://stackoverflow.com/questions/22789356/plot-a-donut-with-fill-or-fill-between-use-pyplot-in-matplotlib
ton10's answer
"""
x = np.linspace(-outer, outer, 300, endpoint=True)
yO = outer * np.sin(np.arccos(x/ outer )) # x-axis values -> outer circle
yI = inner * np.sin(np.arccos(x/ inner )) # x-axis values -> inner circle (with nan's beyond circle)
yI[np.isnan(yI)] = 0. # yI now looks like a boulder hat, meeting yO at the outer points
ax = plt.subplot(111)
ax.fill_between(x+pos[0], yI+pos[1], yO+pos[1], color=color)
ax.fill_between(x+pos[0], -yO+pos[1], -yI+pos[1], color=color)
plt.show()
#
def plot_circle(r, pos):
""" REF: https://math.stackexchange.com/questions/260096/find-the-coordinates-of-a-point-on-a-circle """
arrx = []
arry = []
for theta in xrange(1000):
x,y = r * math.sin(theta), r * math.cos(theta)
arrx.append(x)
arry.append(y)
#
plt.plot(arrx, arry, color='red')
plt.show()
#
#r = 3
#pos = 2,2
#plot_circle(r, pos)
r1, r2 = 2, 2.1
position = [4,2]
color = 'b'
plot_circle_donut(position, r1, r2, color)
1
这有点像小技巧,但下面这个方法可以实现:
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)
fig, ax = plt.subplots()
ax.fill_between(x2, -y2, y2, color='red')
ax.fill_between(x1, y1, -y1, color='white')
plt.show()
它会把你甜甜圈的整个区域画成红色,然后再把中间的“洞”画成白色。
3
你可以通过分别绘制上半部分和下半部分来实现这个效果:
import numpy as np
import matplotlib.pyplot as plt
inner = 5.
outer = 10.
x = np.linspace(-outer, outer, 1000, endpoint=True)
yO = outer*np.sin(np.arccos(x/outer)) # x-axis values -> outer circle
yI = inner*np.sin(np.arccos(x/inner)) # x-axis values -> inner circle (with nan's beyond circle)
yI[np.isnan(yI)] = 0. # yI now looks like a boulder hat, meeting yO at the outer points
ax = plt.subplot(111)
ax.fill_between(x, yI, yO, color="red")
ax.fill_between(x, -yO, -yI, color="red")
plt.show()
或者你也可以使用极坐标来做,不过这是否有好处要看具体情况:
import numpy as np
import matplotlib.pyplot as plt
theta = np.linspace(0., 2.*np.pi, 80, endpoint=True)
ax = plt.subplot(111, polar=True)
ax.fill_between(theta, 5., 10., color="red")
plt.show()
17
你可以沿着一个封闭曲线的边界进行遍历,然后使用 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()
这个方法可以很容易地应用到任何形状上,比如说一个五边形可以在圆的内部或外部: