Home > Software engineering >  Python matplotlib: how to plot vertical bars with both a bottom and a top value
Python matplotlib: how to plot vertical bars with both a bottom and a top value

Time:06-29

Is there a way to plot bars which do not all start from the same baseline and have their own top value, but rather specify both a bottom and a top value?

In other words if I had the following dataframe:

import pandas as pd
data={'Seconds':[10,20,30,40],'SYS':[95,103,99,112],'DIA':[56,75,62,70]}
df = pd.DataFrame(data)

and from this I would need to show 4 bars, with the 'Seconds" values on the X axis and four bars which would start with at the df['DIA'] value and the top of the bar at the df['SYS'] value.

Is it possible? Thank you

CodePudding user response:

You can do the following:

from matplotlib import pyplot

pyplot.bar(
    x=df['Seconds'],
    height=df['SYS'] - df['DIA'],
    bottom=df['DIA'],
)

output:

enter image description here

  • Related