如何围绕散点图上的一个点绘制圆环图?
How to plot a donut chart around a point on a scatterplot?
我有一个包含几个点的散点图,我可以很容易地绘制这些点。我想在每个点周围添加一个圆环图,以指示哪个 类 构成了该点。我看到了 nested donut charts 的例子,但我想为多个点制作一个 scatter/donut 图。
这是我目前制作散点图和圆环图的代码。它将绘制所有 3 个数据点和一个圆环图作为第一个点。
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
## Scatter
# create three data points with three random class makeups
N = 3
N_class = 5
x = np.random.rand(N)
y = np.random.rand(N)
vals = [np.random.randint(2, size=N_class) for _ in range(N)]
plt.scatter(x, y, s=500)
plt.show()
## Donut plot
# Create 5 equal sized wedges
size_of_groups = np.ones(5)
# Create a pieplot
plt.pie(size_of_groups, colors=["grey" if val == 0 else "red" for val in vals[0]])
#plt.show()
# add a circle at the center
my_circle=plt.Circle( (0,0), 0.7, color='white')
p = plt.gcf()
p.gca().add_artist(my_circle)
plt.show()
每个点都与此类似(忽略饼图中心,只是一个散点)
适配Scatter plot with pie chart markers example,只需在中间加一个白色标记,馅饼就变成了甜甜圈。
import numpy as np
import matplotlib.pyplot as plt
# first define the ratios
r1 = 0.2 # 20%
r2 = r1 + 0.4 # 40%
# define some sizes of the scatter marker
sizes = np.array([60, 80, 120])*4
center_sizes = sizes/3.
# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
xy1 = np.column_stack([x, y])
s1 = np.abs(xy1).max()
x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
xy2 = np.column_stack([x, y])
s2 = np.abs(xy2).max()
x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
xy3 = np.column_stack([x, y])
s3 = np.abs(xy3).max()
fig, ax = plt.subplots()
ax.scatter(range(3), range(3), marker=xy1,
s=s1 ** 2 * sizes, facecolor='indigo')
ax.scatter(range(3), range(3), marker=xy2,
s=s2 ** 2 * sizes, facecolor='gold')
ax.scatter(range(3), range(3), marker=xy3,
s=s3 ** 2 * sizes, facecolor='crimson')
# centers
ax.scatter(range(3), range(3), s=center_sizes, marker="o", color="w")
plt.show()
如果需要真正的 pie
图表,您可以使用参数 center
和 radius
在坐标轴上放置多个饼图。
import matplotlib.pyplot as plt
# first define the ratios
r1 = 0.2 # 20%
r2 = r1 + 0.4 # 40%
x = list(range(3))
y = list(range(3))
fig, ax = plt.subplots()
for xi,yi in zip(x,y):
ax.pie([r1,r2,r2], colors=['indigo', "gold", 'crimson'],
center=(xi, yi), radius=0.2+xi/4,
wedgeprops=dict(width=(0.2+xi/4)/2), frame=True)
ax.autoscale()
plt.show()
我有一个包含几个点的散点图,我可以很容易地绘制这些点。我想在每个点周围添加一个圆环图,以指示哪个 类 构成了该点。我看到了 nested donut charts 的例子,但我想为多个点制作一个 scatter/donut 图。
这是我目前制作散点图和圆环图的代码。它将绘制所有 3 个数据点和一个圆环图作为第一个点。
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
## Scatter
# create three data points with three random class makeups
N = 3
N_class = 5
x = np.random.rand(N)
y = np.random.rand(N)
vals = [np.random.randint(2, size=N_class) for _ in range(N)]
plt.scatter(x, y, s=500)
plt.show()
## Donut plot
# Create 5 equal sized wedges
size_of_groups = np.ones(5)
# Create a pieplot
plt.pie(size_of_groups, colors=["grey" if val == 0 else "red" for val in vals[0]])
#plt.show()
# add a circle at the center
my_circle=plt.Circle( (0,0), 0.7, color='white')
p = plt.gcf()
p.gca().add_artist(my_circle)
plt.show()
每个点都与此类似(忽略饼图中心,只是一个散点)
适配Scatter plot with pie chart markers example,只需在中间加一个白色标记,馅饼就变成了甜甜圈。
import numpy as np
import matplotlib.pyplot as plt
# first define the ratios
r1 = 0.2 # 20%
r2 = r1 + 0.4 # 40%
# define some sizes of the scatter marker
sizes = np.array([60, 80, 120])*4
center_sizes = sizes/3.
# calculate the points of the first pie marker
#
# these are just the origin (0,0) +
# some points on a circle cos,sin
x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
xy1 = np.column_stack([x, y])
s1 = np.abs(xy1).max()
x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
xy2 = np.column_stack([x, y])
s2 = np.abs(xy2).max()
x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
xy3 = np.column_stack([x, y])
s3 = np.abs(xy3).max()
fig, ax = plt.subplots()
ax.scatter(range(3), range(3), marker=xy1,
s=s1 ** 2 * sizes, facecolor='indigo')
ax.scatter(range(3), range(3), marker=xy2,
s=s2 ** 2 * sizes, facecolor='gold')
ax.scatter(range(3), range(3), marker=xy3,
s=s3 ** 2 * sizes, facecolor='crimson')
# centers
ax.scatter(range(3), range(3), s=center_sizes, marker="o", color="w")
plt.show()
如果需要真正的 pie
图表,您可以使用参数 center
和 radius
在坐标轴上放置多个饼图。
import matplotlib.pyplot as plt
# first define the ratios
r1 = 0.2 # 20%
r2 = r1 + 0.4 # 40%
x = list(range(3))
y = list(range(3))
fig, ax = plt.subplots()
for xi,yi in zip(x,y):
ax.pie([r1,r2,r2], colors=['indigo', "gold", 'crimson'],
center=(xi, yi), radius=0.2+xi/4,
wedgeprops=dict(width=(0.2+xi/4)/2), frame=True)
ax.autoscale()
plt.show()