简体   繁体   English

情节:饼图只显示图例

[英]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.然后我使用df.to_dict()将它转储到 dict 中, df.to_dict()于重现。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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