Dataset:
Year , Store_type , MY_SHOPPING ,TRAN_SPEND
2012, LARGE_STORE , HEALTH CONSCIOUS , 49383.70$
2012 , CONVENIENCE_STORE , CONFIDENT COOKS , 13150.00$
2013 , LARGER_STORE , QUICK&EASY , 98765.00$
2013 , LARGER_STORE , TRADITIONAL , 45734.00$
Question: I want to have a pie chart that represents the year in the middle of the chart and have a label = store_type and display percentage of different categories in MY_SHOPPING column plus the name of that and also the TRAN_SPEND amount in dollar
My code:
fig = px.pie(removed_index_storeformat, values='TRAN_SPEND', names='STORE_TYPE',
hover_data=['MY_SHOPPING '], labels={'MY_SHOPPING ':'shop'})
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()
Donut pie charts are made possible by setting the hole size. Also, use annotations to put text strings inside the Add a numerical column separate from the column of strings marked with dollar signs in the data presented. Use the numerical column for the pie chart and the original dollar-marked column for the text.
import pandas as pd
import numpy as np
import io
data = '''
Year,Store_type,MY_SHOPPING,TRAN_SPEND
2012,LARGE_STORE,HEALTH CONSCIOUS,49383.70$
2012,CONVENIENCE_STORE,CONFIDENT COOKS,13150.00$
2013,LARGER_STORE,QUICK&EASY,98765.00$
2013,LARGER_STORE,TRADITIONAL,45734.00$
'''
df = pd.read_csv(io.StringIO(data), sep=',')
df['TRAN_SPEND2'] = df['TRAN_SPEND'].apply(lambda x: float(x[:-1]))
df
Year Store_type MY_SHOPPING TRAN_SPEND TRAN_SPEND2
0 2012 LARGE_STORE HEALTH CONSCIOUS 49383.70$ 49383.7
1 2012 CONVENIENCE_STORE CONFIDENT COOKS 13150.00$ 13150.0
2 2013 LARGER_STORE QUICK&EASY 98765.00$ 98765.0
3 2013 LARGER_STORE TRADITIONAL 45734.00$ 45734.0
import plotly.express as px
dff = df.query('Year == 2012')
fig = px.pie(dff, values='TRAN_SPEND2', names='Store_type',
hover_data=['MY_SHOPPING'], labels={'MY_SHOPPING ':'shop'}, hole=0.3)
fig.update_traces(textposition='inside', text=dff['TRAN_SPEND'], textinfo='percent+label+text')
fig.update_layout(
title_text="Test Title",
annotations=[dict(text='2012', x=0.5, y=0.5, font_size=20, showarrow=False)])
fig.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.