用标签绘制来自Pandas DataFrame的多个饼图

发布于 2025-02-11 04:57:47 字数 1020 浏览 0 评论 0原文

我正在尝试从单个数据框架中绘制多个饼图,

因为plotly.express.pie没有刻度功能,我使用了两个标准(datframe中的两个不同的列),我使用了plotly.graph_objects,但我不知道如何将结果重新格式化为带有标签的可读表单。

我愿意使用任何生成整齐地块的绘图库。

我的代码:

l = []
for q in range(len(pollutants)):
    l.append({'type':'domain'})
specs = [l]*len(country_codes)
fig = make_subplots(rows=len(country_codes), cols=len(pollutants), specs=specs)
for i, cc in enumerate(country_codes):
    for j, p in enumerate(pollutants):
        df_slice = df.loc[(df.country_code == cc) & (df.pollutant == p)].sort_values('count')
        values = df_slice['count']
        labels = [str(x) for x in df_slice['category']]
        fig.add_trace(go.Pie(labels=labels, 
                             values=values, 
                             name=f"{cc} : {p}"), 
                      i+1, j+1)
fig.update_traces(hole=0.5, hoverinfo="label+percent+name")

这是结果: 用plotly.graph_obects绘制的多个饼图

I am trying to plot multiple pie charts from a single dataframe, separated by two criteria (two different columns in the datframe)

since plotly.express.pie doesn't have the faceting function, I used plotly.graph_objects but I can't figure out how to re-format the result from a hot mess to a readable form with labels.

I am open to using any and all plotting libraries that produce neat plots.

My code:

l = []
for q in range(len(pollutants)):
    l.append({'type':'domain'})
specs = [l]*len(country_codes)
fig = make_subplots(rows=len(country_codes), cols=len(pollutants), specs=specs)
for i, cc in enumerate(country_codes):
    for j, p in enumerate(pollutants):
        df_slice = df.loc[(df.country_code == cc) & (df.pollutant == p)].sort_values('count')
        values = df_slice['count']
        labels = [str(x) for x in df_slice['category']]
        fig.add_trace(go.Pie(labels=labels, 
                             values=values, 
                             name=f"{cc} : {p}"), 
                      i+1, j+1)
fig.update_traces(hole=0.5, hoverinfo="label+percent+name")

Here's the result:
multiple pie charts plotted with plotly.graph_obects

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文