用标签绘制来自Pandas DataFrame的多个饼图
我正在尝试从单个数据框架中绘制多个饼图,
因为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")
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论