【发布时间】:2020-10-31 18:34:14
【问题描述】:
我想在不扭曲圆形纵横比的情况下在特定位置绘制彩色饼图。我正在使用 Wedge 补丁,因为我找不到更好的解决方案。这是代码
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patches, collections
fig, axes = plt.subplots()
for i in range(20):
x = np.random.uniform(low=0, high=1, size=10).cumsum()
axes.scatter(x=x, y=np.repeat(i, x.shape[0]), c='gray', s=1)
pies = []
N = 4
cmap = plt.cm.get_cmap("hsv", N + 1)
colors = list(map(cmap, range(N)))
print(colors)
for i in range(2, 2 + N):
thetas = np.linspace(0, 360, num=i)
assert len(thetas) - 1 <= len(colors)
for theta1, theta2, c in zip(thetas[:-1], thetas[1:], colors):
wedge = patches.Wedge((i, i), r=i / 10, theta1=theta1, theta2=theta2,
color=c)
pies.append(wedge)
axes.add_collection(collections.PatchCollection(pies,
match_original=True))
plt.show()
如何保持饼图的纵横比?设置axes.set_aspect("equal") 不是一个选项,因为当我有更多数据点时,它会完全压缩绘图。
我一直在查看 how to draw circles and preserve the aspect ratio,但这里无法采用该解决方案 - 我正在绘制楔形/饼图,而不是圆形。
我也查看了matplotlib transforms,但在那里也找不到答案。
【问题讨论】:
标签: python matplotlib pie-chart