# 6.2.3 Advanced Pie Chart

### Pie Chart

In chapter 2, we discussed why the pie chart is easy to be messed up. Here is another example. When the observations are more than 5, the pie chart will become hard to read or deliver information. You should be very careful before using it.&#x20;

```
df = px.data.gapminder().query("year == 2007").query("continent == 'Asia'")
df.loc[df['pop'] < 10.e6, 'country'] = 'Other countries' # Represent only large countries
fig = px.pie(df, values='pop', names='country', title='Population of Asia continent',width=800, height=400)
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MD1Fhyja1pkmu1nlozO%2F-MD1GSY-OJ8vD6FIISaB%2Fasian%20population.png?alt=media\&token=57024f06-76a7-4ff3-b3a0-753b8ffe39dd)

### Donut Chart

Let's start with a simple donut chart.

```
import plotly.graph_objects as go

labels = ['Apple','Banana','Kiwi','Grape']
values = [3000, 5000, 2000, 2500]

# Use `hole` to create a donut-like pie chart
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)])
fig.show()
```

![Simple Donut Chart](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDM8shETaIDJ0CAYqTk%2F-MDMCTI4g53pvZ8PPRAo%2FScreenshot%202020-07-28%20at%2022.51.31.png?alt=media\&token=46e3f3d6-5a02-48ab-9bda-959c04d00904)

This donut chart is not sweet enough, we can add some customization to make it look better. For example,

* change the color set
* make the font size bigger `update_traces`
* make the donut hole bigger `hole`
* cut a slice out `pull`
* put the legend closer to the donut `update_layout`

```
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.5, pull=[0, 0.1, 0, 0])])
colors = ['gold', 'dodgerblue', 'tomato', 'lightgreen']

fig.update_traces(hoverinfo='label+percent', textinfo='value', 
                  textfont_size=16,marker=dict(colors=colors))
fig.update_layout(legend=dict(orientation="h",yanchor="bottom",y=1.02,
    xanchor="right",x=0.7))
fig.show()
```

![A Sweeter Donut ](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MDM8shETaIDJ0CAYqTk%2F-MDMCTI8eY7zT4In9c7I%2FScreenshot%202020-07-28%20at%2022.54.55.png?alt=media\&token=169b6b61-556f-44b2-bdf5-6723c02ff384)

### Sunburst Chart

**Sunburst plots** visualize hierarchical data spanning outwards radially from root to leaves. It looks fancy and stylish, however, it could be even messer than a pie chart. Be careful!

```
fig = px.sunburst(df, path=['continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'])
fig.show()
```

![](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MCqVqsamRSvSjsuWxPb%2F-MCqxE79NZJYnv5jTpaP%2FScreenshot%202020-07-22%20at%2016.36.40.png?alt=media\&token=c4a7bf9e-938f-4502-b03f-89000068f327)
