如何使用绘图脱机从一个数据框中绘制多个饼图

时间:2020-10-30 08:04:57

标签: python dataframe charts plotly

我有一个要绘制多个饼图的数据框

                            index       number_person
event_mohafaza event_type
loc1           type1           0             25
               type2          33              0
               type3          12              0
loc2           type1          29             25
               type2           1             20
loc3           type1          19             40
               type2         137             15
loc4           type1         133             80
               type2          21             40
loc5           type1         155            100
               type2         186            150

我想基于 event_mohafaza列

将结果绘制为5个饼图

所以预期结果将是:

  • 饼图1必须显示:

3用number_person滑动type1 type2 type3。

  • 饼图2必须显示:

2用number_person滑动type1 type2。

直到现在,我能够绘制一张饼图,在一张饼图中显示所有值。

代码:

import pandas as pd
import numpy as np
import cufflinks as cf
import plotly.offline
cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)

from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go

 ct = df.groupby([df['event_mohafaza'],df['event_type']]).aggregate('sum')
 ct = ct.reset_index()
 ct['labels'] = ct['event_mohafaza'] + ' ' + ct['event_type']
                        
#### plot the pie chart
trace = go.Pie(labels=ct['labels'], 
hovertemplate = "%{label}: <br>Value: %{value} ",
textinfo = 'percent',
values=ct[selected_item],
hole=.4,
rotation=90)

layout = go.Layout(
title="Percentage of {} ".format(item1),
font=dict(family='Arial', size=12, color='#909090'),
legend=dict(x=0.9, y=0.5)
)
data = [trace]
print(data)
figure = go.Figure(data=data, layout=layout)
plotly.offline.plot(figure)
                

0 个答案:

没有答案
相关问题