如何在情节中给剔号涂上颜色? [英] How to color ticktext in plotly?
问题描述
我想根据各自的字符串(基于字典)在不同的x轴上以不同的颜色显示我的刻度文本.是否有功能可以通过HTML编码实现?
ticktext = ['< font color ="red"> {}</font>'.format(x)for ticktexts中的x]
不起作用,它将html字符串提供给标签.
使用 LaTeX
的一些变通办法可以在这里为您提供帮助(抱歉@Iwileczek,我偷了您的示例,希望您不要这样做).请注意),因为 plotly
具有完整的 LaTeX
2021年3月更新:
如果您不想在输出中使用 MathJax
的 LaTex
呈现的字体,请使用具有HTML样式的@Dapcer解决方案:
def颜色(颜色,文本):返回f"< span style ='color:{str(color)}'>{str(text)}</span>"
带有 fig.update_layout(font = dict(family =" Times New Roman")[...]
的示例:
I would like to display my ticktexts in my plotly xaxis with different colors based on the respective string (based on a dictionary). Is there a functionality in plotly to do this, maybe via HTML coding?
ticktext = ['<font color="red">{}</font> '.format(x) for x in ticktexts]
doesn't work, it gives the html string to the labels.
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") [...]
:
这篇关于如何在情节中给剔号涂上颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!