2
%matplotlib inline
import matplotlib
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'index' : ['A', 'B', 'C', 'D'], 'first': [1.2, 1.23, 1.32, 1.08], 'second': [2, 2.2, 3, 1.08], 'max': [1.5, 3, 0.9, 'NaN']}).set_index('index')

I want to plot a horizontal bar chart with first and second as bars. I want to use the max column for displaying a vertical line at the corresponding values if the other columns.

I only managed the bar plot as for now.

Like this:

enter image description here

1 Answer 1

3

I have replaced the NaN with some finite value and then you can use the following code

df = pd.DataFrame({'index' : ['A', 'B', 'C', 'D'], 'first': [1.2, 1.23, 1.32, 1.08], 
                   'second': [2, 2.2, 3, 1.08], 'max': [1.5, 3, 0.9, 2.5]}).set_index('index')

plt.barh(range(4), df['first'], height=-0.25, align='edge')
plt.barh(range(4), df['second'], height=0.25, align='edge', color='red')
plt.yticks(range(4), df.index);

for i, val in enumerate(df['max']):
    plt.vlines(val, i-0.25, i+0.25, color='limegreen')

enter image description here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.