python - matplotlib:饼图,变量 pctdistance

标签 python matplotlib pie-chart

我希望能够将每个百分比值定位在距离中心的不同距离处,但 pctdistance 需要是单个值。

对于我的情况,pctdistance 应该是一个列表,其中包含生成的距离(由范围生成)。

import matplotlib.pyplot as plt

fig =plt.figure(figsize = (10,10))
ax11 = fig.add_subplot(111)
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 2000]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)  # explode 1st slice

# Plot
w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140, pctdistance=0.8, radius = 0.5)
[t.set_rotation(0) for t in p]
[t.set_fontsize(50) for t in p]
plt.axis('equal')
plt.show()

我有: enter image description here 我想要的是: enter image description here

最佳答案

pie 函数不将列表或数组作为 pctdistance 参数的输入。

您可以使用预定义的 pctdistances 列表手动定位文本。

import numpy as np
import matplotlib.pyplot as plt

fig =plt.figure(figsize = (4,4))
ax11 = fig.add_subplot(111)
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 2000]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']

# Plot
w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140, pctdistance=1, radius = 0.5)

pctdists = [.8, .5, .4, .2]

for t,d in zip(p, pctdists):
    xi,yi = t.get_position()
    ri = np.sqrt(xi**2+yi**2)
    phi = np.arctan2(yi,xi)
    x = d*ri*np.cos(phi)
    y = d*ri*np.sin(phi)
    t.set_position((x,y))

plt.axis('equal')
plt.show()

enter image description here

关于python - matplotlib:饼图,变量 pctdistance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50602158/

相关文章:

javascript - 无法识别同一片谷歌饼图的立即选择

javascript - Plotly.js 饼图值格式

python - 如何使 matplotlib mark_inset 在图表顶部显示线?

Python:没有明显原因的 NameError?

python-2.7 - 没有名为 'matplotlib.pyplot' 的模块; 'matplotlib' 不是包

python - Matplotlib 在绘图中写入 '±'

python - 我自己制作分数课

python - 拆分自相交多边形仅在 Shapely 中返回一个多边形

python - Plt.散点图 : How to add title and xlabel and ylabel

python - 改进子图中饼图的标签