简体   繁体   中英

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. But I want to limit y values between 0 and 1.

If I understand "I want to limit y values between 0 and 1" correctly, pretty straightforward:

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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