A little bit of a workaround using LaTeX
can help you here (sorry @Iwileczek, I stole your example, hope you don't mind) because plotly
has full LaTeX
support:
def color(color, text):
s = '$color{' + str(color) + '}{' + str(text) + '}$'
return s
animals=['giraffes', 'orangutans', 'monkeys']
colors = ['red', 'green', 'yellow', 'blue']
ticks = [5, 10, 15, 20]
keys = dict(zip(ticks, colors))
fig = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])
ticktext = [color(v, k) for k, v in keys.items()]
print(ticktext)
fig.update_layout(
yaxis=dict(tickmode='array', ticktext=ticktext, tickvals=ticks)
)
fig.show()
Update March 2021:
If you don't want to use the LaTex
rendered font by MathJax
in your output, use the solution of @Dapcer with an HTML styling:
def color(color, text):
return f"<span style='color:{str(color)}'> {str(text)} </span>"
Example with fig.update_layout(font=dict(family="Times New Roman") [...]
:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…