pythonplotly

How to change text color of facet category in plotly charts in Python?


I have created few Plotly charts with facets on basis of category variable and would like to change the color of facet text in the chart. Have searched alot even on plotly website but couldn't figure out the property that can be used to change the color for facet text.

Using below image as an example I would like to change the color of - No & Yes:

import plotly.express as px

fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_col="smoker")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.show()

enter image description here

Would really Appreciate any help !!


Solution

  • As for annotations, they are summarized in the layout attributes and can be done by making decisions based on the content of the text. In the following example, NO has been changed to red.

    import plotly.express as px
    
    fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_col="smoker")
    fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
    
    for t in fig.layout.annotations:
        if t.text.split("=")[-1] == 'No':
            t.font.color='red'
            
    fig.show()
    

    enter image description here