Emin Baybars Timur
Emin Baybars Timur

Reputation: 87

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.

Upvotes: 0

Views: 63

Answers (2)

BigBen
BigBen

Reputation: 50162

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

enter image description here

Upvotes: 2

Ewdlam
Ewdlam

Reputation: 935

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

Upvotes: 1

Related Questions