0

this is my simplified code

import numpy as np
import pandas as pd
pd.DataFrame(np.random.randint(1,100,6),index=['1200 am', '1230 am', '0340 pm', '1200 pm', '1030 am', '0140 pm'],columns=['value']).plot(kind='barh')

enter image description here

I want bars for "am" colored in red to distinguish between "am" and "pm", how do I do that?

user13412850
  • 509
  • 1
  • 5
  • 16
  • 3
    Does this answer your question? [Setting Different Bar color in matplotlib Python](https://stackoverflow.com/questions/18973404/setting-different-bar-color-in-matplotlib-python) – BigBen Feb 11 '22 at 16:40

1 Answers1

0

You can plot your data in two passes using a mask like so:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(1,100,6), 
             index=['1200 am', '1230 am', '0340 pm', '1200 pm', '1030 am', '0140 pm'],
             columns=['value'])

m = df.index.str.contains('am')
ax = df.value.mask(m).plot(kind='barh', label='pm', legend=True)
df.value.mask(~m).plot(kind='barh', color='r', ax=ax, label='am', legend=True)

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52