我正在尝试在 Python 中使用 plotly 构建一个堆积条形图。
我的目标是为哥本哈根的每个城区制作一个酒吧,在那里堆叠不同的树类型。我可以运行我的代码直到第 10 棵树,之后我得到一个 IndexError:
单个位置索引器越界
当我只种植 10 棵树或更少时,一切看起来都很好。
我的代码如下所示
## df is a dataframe where each row is a tree type
## and each column a city district
x = df.columns ## district names
trees = df.index ## tree names
for i in range(0,len(trees)):
if i == 0:
fig = go.Figure(go.Bar(x=x, y=df.iloc[:,i].values, name = trees[i]))
else:
fig.add_trace(go.Bar(x=x, y=df.iloc[:,i].values, name = trees[i]))
fig.update_layout(barmode='stack')
fig.show()
我似乎无法弄清楚错误是什么..
乍一看,您似乎混淆了列和行。如果 Kaupmannahöfn 只有 9 个区,但您有 10 种或更多树类型,那么您可能应该使用y=df.iloc[i,:]
按区绘制树的数量。但可以肯定的是,请提供数据框的尺寸、从数据框中选择的几行以及前 9 种树类型的条形图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句