Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
HamidBee
Power Participant
Power Participant

Power BI-Conditional Colour Formatting in Python

I have a bar chart and I am trying to create a custom colour rule whereby if the colour is greater than 0 then green if it is less than 0 then red. I have never used conditional formatting in Python but this is the base code that I have:

 

import pandas as pd

from matplotlib import pyplot as plt

plt.bar(dataset.Type,dataset.Amount, color="green")

plt.show()

 

Thanks in advance.

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @HamidBee 

 

You can try this

import pandas as pd

from matplotlib import pyplot as plt

colors = []

for amount in dataset.Amount:
    if amount >= 0:
        colors.append("green")
    else:
        colors.append("red")

plt.bar(dataset.Type,dataset.Amount, color=colors)

plt.show()

22010601.jpg

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @HamidBee 

 

You can try this

import pandas as pd

from matplotlib import pyplot as plt

colors = []

for amount in dataset.Amount:
    if amount >= 0:
        colors.append("green")
    else:
        colors.append("red")

plt.bar(dataset.Type,dataset.Amount, color=colors)

plt.show()

22010601.jpg

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Much appreciated. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors