How to set tick text to be color red for only negative values?

I am running python 3.12.4 dash 2.17.1 dash_ag_grid 31.2.0 and plotly 5.22.0 on linux OpenSUSE Tumbleweed

I am using Plotly Graphical Objects to build a scatter chart.

I would like to format the tick text in the y-axis of the chart so that the negative values are in red while zero and positive values remain formatted in black. I would like to do this for readability and in order that the plotly charts use the same conventions as have been used in the dash ag grid tables with which the charts will be presented.

I have successfully used the tickfont to format the tick label colors, but I cannot see how to set individual tick label colors differently.

I have searched extensively but not found any answer. Apologies if I missed where if this is covered elsewhere.

Any help is much appreciated.

Gato

@gatomulato It is possible to set the tick label color according to the sign of tick values only with a workaround:

import plotly.graph_objects as go
import numpy as np
x = list(range(0,10))
y = np.random.randint(-5, 6, 13)
tickvals = np.arange(y.min(), y.max()+0.5)
fig = go.Figure(go.Scatter(x=x, y=y))
#workaround to set the required colors for tick labels
tick_labels = []
for tick in tickvals:
    if tick < 0:
        label = f'<span style="color:red">{tick}</span>'
    else:
        label = str(tick)
    tick_labels.append(label)
fig.update_yaxes(tickvals=tickvals, 
                 ticktext=tick_labels,
                 tickfont_color='black') #the default font color is set to "black"
fig.update_layout(width=600, height=450) 

color-labels

1 Like

Excellent. Very helpful. Especially as I am already using tickvals and ticktext for other formatting workarounds. I confirm it works.

Thank you.

Gato