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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

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
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

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.