Home > Back-end >  pandas dataframe plot barh color based on values not column
pandas dataframe plot barh color based on values not column

Time:08-10

I have this dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame(
    data={
        "Time": ['07/10/2022 08:22:44','07/10/2022 08:27:22','07/10/2022 08:27:44','07/10/2022 08:29:55','07/10/2022 08:33:14','07/10/2022 08:48:44'],
        "Sum":[50,5,10,20,5,30]
    }   
) 

and I am trying to create a barh where the index would be set to 'Time' and the color of the bars is based on the value 'Sum'...I was thinking something like this:

df=df.set_index('Time')

colors=[]
for val in df['Sum']:
    if val <= 10:
        colors.append('green')
    elif val > 10 & val <=20:
        colors.append('orange')
    else:
        colors.append('red')
ax = df.plot(kind='barh',color=colors)

but all I'm getting is the color green. What am I doing wrong?

CodePudding user response:

You can try Series.plot with color argument

colors=[]
for val in df['Sum']:
    if val <= 10:
        colors.append('green')
    elif 10 < val <=20:
        colors.append('orange')
    else:
        colors.append('red')

# or
colors = pd.cut(df['Sum'].tolist(), [-np.inf, 10, 20, np.inf],
                labels=['green', 'orange', 'red'])


ax = df['Sum'].plot(kind='barh',color=colors)

enter image description here

CodePudding user response:

Try this

df=df.set_index('Time')
colors=[]
for val in df['Sum']:
    if val <= 10:
        colors.append('green')
    elif (val > 10) & (val <=20):
        colors.append('orange')
    else:
        colors.append('red')

plt.barh(df.index, df['Sum'],color=colors)
plt.show()

this will give you what you want. Carefull with the orange condition, you need to wrap in parenthesis to do the proper boolean check

  • Related