Files
training.python.datascience/source/plotting/charts/plotly_sunburst.py
2025-10-24 22:29:15 +02:00

26 lines
1.2 KiB
Python

"""
Display sales of various cities in a sunburst chart.
Given the cities can have recurring parents, we can have
a sunburst chart with 2 rings, the centermost ring for the
country, and the outmost ring to have sales per city.
"""
import pandas as pd
import plotly
from plotly.express import sunburst
if __name__ == '__main__':
df = pd.DataFrame(data={
"country": ["France", "France", "Spain", "Spain", "England", "England", "England"],
"city": ["Montpellier", "Bordeaux", "Madrid", "Valencia", "London", "Manchester", "Bristol"],
"sales": [150_000, 127_000, 97_200, 137_250, 200_000, 180_000, 150_000]
})
plot = sunburst(df, path=["country", "city"], values="sales", title="Sales by country and city", template="ggplot2",
color_discrete_sequence=plotly.colors.qualitative.Dark2)
sb = next(plot.select_traces(row=0, col=0))
sb.textinfo="label+percent parent"
# sb.texttemplate="%{label}<br>%{value:.2f}€"
plot.layout.update({"font": {"family": "Cabin", "size": 13}, "showlegend": True})
# plot.update_traces(textinfo="label+percent parent", texttemplate="%{label}<br>%{value:.2f}€")
plot.show(renderer="browser")