创建堆积条形图

Creating a stacked bar plot

我有这样的数据:

data = {'Accuracies': [0.52,0.56,0.55,0.57], 'd Primes':[0.06, 0.12, 0.09,0.15]}

defe = pd.DataFrame(data, index= ['1400', "2000", "3000", "4000"])

我想用 x 轴的索引和 y 轴的精度来绘制它。 但我想将 y 值限制在 0 到 1 之间。

如果我理解 "I want to limit y values between 0 and 1" 正确,非常简单:

ax = defe.plot.bar(stacked=True)
ax.set_ylim(0, 1)

或者只是

defe.plot.bar(stacked=True, ylim=(0,1))

plotly 的另一种解决方案,与使用 matplotlib 相比,它可以让您自定义图形更多,尤其是悬停

import plotly.graph_objects as go

df_graph = defe[(defe['Accuracies']>= 0) & (defe['Accuracies']<= 1)]

fig = go.Figure(data=[
    go.Bar(name='Accuracies', x=df_graph.index.values, y=df_graph['Accuracies'].values),
    go.Bar(name='d Primes', x=df_graph.index.values, y=df_graph['d Primes'].values)
])

fig.update_layout(barmode='stack')
fig.show()