我有一个Dataframe,其数据如下:
data={'A': {'2020_01': 3, '2020_02': 3, '2020_03': 1, '2020_04': 3, '2020_05': 1},
'B': {'2020_01': 0, '2020_02': 0, '2020_03': 3, '2020_04': 0, '2020_05': 2},
'other': {'2020_01': 0,
'2020_02': 0,
'2020_03': 3,
'2020_04': 0,
'2020_05': 2},
'total': {'2020_01': 3,
'2020_02': 3,
'2020_03': 7,
'2020_04': 3,
'2020_05': 5}}
df = pd.DataFrame(data)我想用X来表示A,B,other的日期和y叠加值
只有一个酒吧我能做到:
import plotly.express as px
fig = px.bar(df, x=df.index, y='A', text_auto=True,
labels={'A':'bananas'}, height=400)
fig.show()我要怎么做?我查了一堆文件,但没有结果:https://community.plotly.com/t/plotly-express-bar-ignores-barmode-group/31511 https://plotly.com/python/bar-charts/ Plotly px.bar not stacking with barmode='stack' .结果:

发布于 2022-10-28 13:34:40
所有你应该做的事情如下:
melt函数以适当的形式转换数据。。
barmode="stack"绘制堆叠条形图。将plotly.express导入为px df.reset_index(inplace=True) df = df.iloc:,:-1.熔体(id_vars=‘index’,var_name=‘类别’,value_name='count')图= px.histogram(df,x=“索引”,y=“计数”,text_auto=True,color=“类别”,barmode=“堆栈”,labels={‘A’:‘香蕉’},height=400) fig.show()
输出:

https://stackoverflow.com/questions/74235638
复制相似问题