饼图,如何在图表旁边放置百分比?
Pie chart, how to put percentages next to graph?
我试图创建一个饼图,在图表旁边显示百分比。我拥有的数据如下。
users = [80, 40, 1000, 300, 50, 80, 10]
os = ['MacOS', 'Chrome', 'Windows', 'Linux', 'Devian', 'Ubuntu', 'Arch Linux']
我正在尝试得到这样的东西。
感谢您的帮助!
尝试将 autopct
设置为您需要的值:
plt.pie(users,
labels=os,
explode=[0, 0, 0.05, 0, 0, 0, 0],
pctdistance = 1.2,
labeldistance = 1.4,
autopct=lambda x: f'{x:.1f}%\n({(x/100)*sum(users):.0f} users)',
textprops={"family": "Arial", "size": 12},
radius = 2,
colors = ["#9BC2E6", "#FF6600", "#F4B084", "#00B050", "#C6E0B4", "#8633FF", "#CCCCFF"]
)
plt.legend(loc="best", bbox_to_anchor=(2.5,0), title="Operating systems")
输出:
尝试使用 plotly.express
:
import plotly.express as px
users = [80, 40, 1000, 300, 50, 80, 10]
os = ['MacOS', 'Chrome', 'Windows', 'Linux', 'Devian', 'Ubuntu', 'Arch Linux']
fig = px.pie(values=users, names=os,
color_discrete_sequence=px.colors.sequential.RdBu)
fig.update_traces(textposition='outside',
textinfo='percent+label+value',
marker=dict(line=dict(color='#FFFFFF', width=2)),
textfont_size=12)
fig.show()
效果还不错:
我试图创建一个饼图,在图表旁边显示百分比。我拥有的数据如下。
users = [80, 40, 1000, 300, 50, 80, 10]
os = ['MacOS', 'Chrome', 'Windows', 'Linux', 'Devian', 'Ubuntu', 'Arch Linux']
我正在尝试得到这样的东西。
感谢您的帮助!
尝试将 autopct
设置为您需要的值:
plt.pie(users,
labels=os,
explode=[0, 0, 0.05, 0, 0, 0, 0],
pctdistance = 1.2,
labeldistance = 1.4,
autopct=lambda x: f'{x:.1f}%\n({(x/100)*sum(users):.0f} users)',
textprops={"family": "Arial", "size": 12},
radius = 2,
colors = ["#9BC2E6", "#FF6600", "#F4B084", "#00B050", "#C6E0B4", "#8633FF", "#CCCCFF"]
)
plt.legend(loc="best", bbox_to_anchor=(2.5,0), title="Operating systems")
输出:
尝试使用 plotly.express
:
import plotly.express as px
users = [80, 40, 1000, 300, 50, 80, 10]
os = ['MacOS', 'Chrome', 'Windows', 'Linux', 'Devian', 'Ubuntu', 'Arch Linux']
fig = px.pie(values=users, names=os,
color_discrete_sequence=px.colors.sequential.RdBu)
fig.update_traces(textposition='outside',
textinfo='percent+label+value',
marker=dict(line=dict(color='#FFFFFF', width=2)),
textfont_size=12)
fig.show()
效果还不错: