-1

I have the following code to create a plotly pie chart

fig6 = px.pie(df_final, values='Price', names='Domain',
              title='Total Price')
fig6.update_traces(textposition='inside', textinfo='percent+label+value')
fig6.show()

The values ("Price") is in millions and it shows as such in the pie chart. For example, 23,650,555 is one price value on the pie chart. However I would like to round it up to 24 (million). I would like to do that for every pie in the pie chart.

Thanks,

MikeA
  • 21
  • 6

1 Answers1

0

You could perform a roundup action to Price column.

import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    "Price": [23650555, 31483589, 10473875, 56346772],
    "Domain": ["3", "1", "0", "6"],
})

def roundup(x):
    return int(math.ceil(x / 1000000.0)) * 1000000

def absolute_value(val):
    a  = np.round(val/100.*df['Price'].sum(), 0)
    return roundup(a)

print(df)
#       Price Domain
# 0  23650555      3
# 1  31483589      1
# 2  10473875      0
# 3  56346772      6

df['Price'] = df['Price'].apply(roundup)
df.set_index(['Domain'], inplace=True)

print(df)
#       Price Domain
# 0  24000000      3
# 1  32000000      1
# 2  11000000      0
# 3  57000000      6


df.plot(kind='pie', y='Price', autopct=absolute_value)

plt.show()

enter image description here

Reference: Python round up integer to next hundred

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52