Plotly:饼图仅显示图例
Plotly: Pie chart only shows the legend
我正在尝试在 plotly 中创建饼图。我有与此类似的数据框:
>>> 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
我正在尝试用这样的情节创建图表:
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()
但我得到的图表只显示图例,没有饼图本身。
我已经用这个脚本创建了其他饼图,所以我没有找到它不能正确生成饼图的原因。
这可能是什么原因造成的?
我相当确定这纯粹是数据问题。我将您的示例数据编辑为逗号分隔版本:
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
然后我使用 df.to_dict()
将其转储到字典中以便于重现。现在,下面的完整代码片段可以毫无问题地生成以下图:
情节
代码
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()
我正在尝试在 plotly 中创建饼图。我有与此类似的数据框:
>>> 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
我正在尝试用这样的情节创建图表:
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()
但我得到的图表只显示图例,没有饼图本身。
我已经用这个脚本创建了其他饼图,所以我没有找到它不能正确生成饼图的原因。
这可能是什么原因造成的?
我相当确定这纯粹是数据问题。我将您的示例数据编辑为逗号分隔版本:
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
然后我使用 df.to_dict()
将其转储到字典中以便于重现。现在,下面的完整代码片段可以毫无问题地生成以下图:
情节
代码
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()