Python: Hvplot negative values coloring

104 Views Asked by At

I try to do interactive bar chart with hvplot, but I would like to color negative values.

enter image description here

I tried using cmap but it colors positives values negatively too.

enter image description here

1

There are 1 best solutions below

0
Adrien C On

You can use a compositional HoloView plot for this.

plot1 = df[df['Results']>0].hvplot.bar(y='Results')
plot2 = df[df['Results']<0].hvplot.bar(y='Results')

plot1*plot2

Note: sorry I can't post images directly yet as this is my first entry on stackoverflow, but this link will show you the plot result https://i.stack.imgur.com/bnoFg.png

Here the full code below, you can adapt it to your dataframe :

# create a dataframe with column 'Name' as index
dict = {'Name':["Rick", "Sam", "Kelly", "Al"],
        'Results':[-90, +40, +80, -28]}

df = pd.DataFrame(dict)
df.index=df['Name']

# create 2 hvplots: 1 for positive results, and 1 for negative results 
plot1 = df[df['Results']>0].hvplot.bar(y='Results')
plot2 = df[df['Results']<0].hvplot.bar(y='Results')

# layout plot1 and plot2 content on the same frame using a compositional plot 
plot1*plot2