我正在尝试绘制一个饼图。我有与此类似的数据框:
mappedSections: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);
mappedSecSub = this.mappedSections.asObservable()
targetSection: { id, name, parentId, text };
targetSectionGroup: { id, name, parentId, text }[] = [];
ngOnInit(): void {
this.fetchData();
}
fetchData = () => {
this.HelpService.getHelp().subscribe(res => {
this.mappedSections.next(res['res'])
let newMappedSection = this.mappedSections.getValue()
for (let i = 0; i < newMappedSection.length; i++) {
const element = newMappedSection[i];
if (element.parentId) {
this.targetSection = element;
break
}
}
})
}
clicked(id, parentId) {
this.targetSectionGroup = [];
let data = this.mappedSections.getValue()
for (let i = 0; i < data.length; i++) {
if (data[i].parentId == parentId || data[i].id == parentId) {
this.targetSectionGroup.push(data[i]);
}
if (data[i].id === id) {
this.targetSection = data[i]
}
}
document.querySelector(`#s${id}ss${parentId}`).scrollIntoView({ behavior: 'smooth' })
}
我正在尝试使用如下方式创建图表:
>>> 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
但是我得到的图表仅显示图例,而没有饼图本身。
我完全使用此脚本创建了其他饼图,因此在这里找不到它不能正确生成饼图的原因。
这可能是什么原因?
答案 0 :(得分:1)
我相当确定这纯粹是一个数据问题。我已将您的示例数据编辑为逗号分隔的版本:
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()