isic5
isic5

Reputation: 191

creating a Piechart in Plotly/Dash out of a Dataframe/Pivot Table

This is my code, as a quick explanation I am filtering my dataframe to select the correct KPI, in this case Operator Share, the correct country, which is prestored in the variable country and the year 2017. Then I want to create a piechart that uses the KPIvalues of the different specifications. So Specifications should be the labels and KPI values the values.

df_plot_operator_share = df[df['Country'] == Country]
df_plot_operator_share = df.loc[(df['KPIname'].isin(['OperatorShare'])) & (df['YearNb']==2017)]

pv_operator_share = pd.pivot_table(
df_plot_operator_share,
index=['Specification'],
values=['KPIvalue'],
aggfunc=sum,
fill_value=0)

trace1 = go.Pie(
lables= pv_operator_share.index, values= pv_operator_share["KPIvalue"],
name='OperatorShare')
return {
'data': [trace1],
'layout':
go.Layout(
    title='Country: {} Operator Share'.format(Country),
    )

}

The Pivot has the Specifications has the headers and the KPIvalues as its values.

I want to have a pie chart that uses the Specifications as labels and the KPIvalues as values.

But for some reason it is not working.

Upvotes: 0

Views: 6981

Answers (2)

Archana Parmar
Archana Parmar

Reputation: 41

import plotly.express as px

fig = px.pie(df, values='numeric_value', names='column_name', title='Your title')

fig.show()

Upvotes: 2

Dmitriy Kisil
Dmitriy Kisil

Reputation: 2998

You tried to call plotly? For example:

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
trace1 = go.Pie(
                labels = pv_operator_share.index,
                values= pv_operator_share["KPIvalue"],
                name='OperatorShare'
                )
data = [trace1]
layout = go.Layout(
                   title='Country: {} Operator Share'.format(Country),
                   )
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='Plot.html')

Upvotes: 2

Related Questions