简体   繁体   English

创建堆积条 plot

[英]Creating a stacked bar plot

I have a data like this:我有这样的数据:

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"])

I want to plot it with indexes in x-axis and Accuracies in y-axis.我想要 plot 它的 x 轴索引和 y 轴精度。 But I want to limit y values between 0 and 1.但我想将 y 值限制在 0 到 1 之间。

If I understand "I want to limit y values between 0 and 1" correctly, pretty straightforward:如果我正确理解“我想将 y 值限制在 0 到 1 之间”,那么非常简单:

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

Or just要不就

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

在此处输入图像描述

Another solution with plotly that will allow you to customize your graph much more than with matplotlib, especially with hovering 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM