python,如何增加价值以及馅饼图中的百分比

发布于 2025-02-02 14:20:12 字数 577 浏览 1 评论 0原文

我编写了一个代码来生成饼图。在AUTOPCT参数器中,我遵循如何从饼图中删除0%此链接删除了在图中显示的0%。现在,我想要价值以及百分比。有人可以帮我解决这个问题吗?

for df_HPC_trial  in ((listofDF_hpc)):
    if len(df_HPC_trial)!= 0
        sum_column = df_HPC_trial.sum(axis=0,numeric_only=True)
        #print(sum_column)
        df_HPC_trial[df_HPC_trial.columns[2:]].sum().plot.pie(autopct=lambda p: '{:.1f}%'.format(round(p)) if p > 0 else '')
        plt.savefig('Figures/Reasons plot') 
        plt.close()

I have written a code to generate pie plot. In the autopct paramaters I followed How to remove 0% from pie chart this link to remove 0% which was showing up in plot. Now I want value along with the percentage. Can anyone please help me to fix this ?

for df_HPC_trial  in ((listofDF_hpc)):
    if len(df_HPC_trial)!= 0
        sum_column = df_HPC_trial.sum(axis=0,numeric_only=True)
        #print(sum_column)
        df_HPC_trial[df_HPC_trial.columns[2:]].sum().plot.pie(autopct=lambda p: '{:.1f}%'.format(round(p)) if p > 0 else '')
        plt.savefig('Figures/Reasons plot') 
        plt.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2025-02-09 14:20:12

也许您需要创建自己的格式来处理此问题。
以下是工作示例

import matplotlib.pyplot as plt
import pandas as pd

def cust_format(x):
    print(x)
    return '{:.4f}%\n({:.0f})'.format(x, total*x/100)


values = pd.Series([False, False, True, True, True, True])
v_counts = values.value_counts()
total = len(values)
fig = plt.figure()
plt.pie(v_counts, labels=v_counts.index, autopct = cust_format, shadow=True)

Maybe you need to create your own format to handle such issue.
Below is working example

import matplotlib.pyplot as plt
import pandas as pd

def cust_format(x):
    print(x)
    return '{:.4f}%\n({:.0f})'.format(x, total*x/100)


values = pd.Series([False, False, True, True, True, True])
v_counts = values.value_counts()
total = len(values)
fig = plt.figure()
plt.pie(v_counts, labels=v_counts.index, autopct = cust_format, shadow=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文