简体   繁体   中英

Plotly express update of the pie chart

The below code produces a pie chart presented in a picture. I need to add a $ sign after the prices in the pie chart, can someone help me do that? I just started exploring plotly:) Thank you in advance!

pie = data[["room_type","price"]]
pie_chart = pie.groupby(['room_type'], as_index=False).median()

fig3 = px.pie(pie_chart,
          values='price',
          names='room_type',
          color_discrete_sequence =['#de425b','#ef805b','#f7b672'])
fig3.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=17)
fig3.update_layout(
    title_text = 'Median of prices per room type',
    legend_title = 'Room type',
    title_font = dict(family='Courier', size=16, color='black'),
    title_x=0.5,
    font_family="Courier",
    font_color="black",
    legend_title_font_color="black"
   )

饼形图

In the fig.update_traces method , you can pass textinfo='text' instead of textinfo='labels' and then define text to be a list of prices from your DataFrame with a dollar sign as a prefix for each price.

import pandas as pd
import plotly.express as px

# pie = data[["room_type","price"]]
# pie_chart = pie.groupby(['room_type'], as_index=False).median()

## recreate DataFrame
pie_chart = pd.DataFrame({
    'room_type':['Entire room/apt','Private room','Shared room'],
    'price': ['68','35','16']
})

fig3 = px.pie(pie_chart,
          values='price',
          names='room_type',
          color_discrete_sequence =['#de425b','#ef805b','#f7b672'])
## pass a list of values 
fig3.update_traces(
    hoverinfo='label+percent', 
    text=['$' + price for price in pie_chart.price.values],
    textinfo='text', 
    textfont_size=17)
fig3.update_layout(
    title_text = 'Median of prices per room type',
    legend_title = 'Room type',
    title_font = dict(family='Courier', size=16, color='black'),
    title_x=0.5,
    font_family="Courier",
    font_color="black",
    legend_title_font_color="black"
   )
fig3.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM