简体   繁体   中英

Plotly: Pie chart only shows the legend

I'm trying to create a pie chart in plotly. I have dataframe that is similar to this:

>>> land_cover                                         count
1   Closed (>40%) broadleaved evergreen or semidec...   1102
2   Closed broadleaved forest or shrubland permane...   22
3   Closed grassland                                     213
4   Closed to open broadleaved decidous shrubland       3
5   Closed to open broadleaved evergreen or semid...    3480
6   Closed to open herbaceousvegetation (or lichen...   501
7   Closed to open shrubland                           9200
8   Closedto open broadleavedforest regularly floo...   89

I'm trying to create the chart with plotly like this:

import plotly.express as px
import plotly.graph_objects as go


fig = px.pie(df, values='count', names='land_cover', title='land cover')
fig.update_layout(title_x=0.48)

fig.show()

But I get a chart that only shows the legend without the pie chart itself.

在此处输入图片说明

I have created other pie charts exactly with this script so I don't find the reason why here it doesn't generate the pie chart properly.

What can be the cause for this?

I'm fairly certain that this is purely a data problem. I edited your sample data into a comma separated version:

land_cover,count
1,Closed (>40%) broadleaved evergreen or semidec,1102
2,Closed broadleaved forest or shrubland permane,22
3,Closed grassland,213
4,Closed to open broadleaved decidous shrubland,3
5,Closed to open broadleaved evergreen or semid,3480
6,Closed to open herbaceousvegetation (or lichen,501
7,Closed to open shrubland,9200
8,Closedto open broadleavedforest regularly floo,89

And then I dumped it into a dict using df.to_dict() for easier reproducibility. Now the complete code snippet below produces the following plot with no issues:

Plot

在此处输入图片说明

Code

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'land_cover': {1: 'Closed (>40%) broadleaved evergreen or semidec',
                                  2: 'Closed broadleaved forest or shrubland permane',
                                  3: 'Closed grassland',
                                  4: 'Closed to open broadleaved decidous shrubland',
                                  5: 'Closed to open broadleaved evergreen or semid',
                                  6: 'Closed to open herbaceousvegetation (or lichen',
                                  7: 'Closed to open shrubland',
                                  8: 'Closedto open broadleavedforest regularly floo'},
                                 'count': {1: 1102, 2: 22, 3: 213, 4: 3, 5: 3480, 6: 501, 7: 9200, 8: 89}})

fig = px.pie(df, values='count', names='land_cover', title='land cover')
fig.update_layout(title_x=0.48)

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.

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