如何在情节中为ticktext着色?

How to color ticktext in plotly?(如何在情节中为ticktext着色?)
本文介绍了如何在情节中为ticktext着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据各自的字符串(基于字典)在我的 plotly xaxis 中以不同的颜色显示我的 ticktexts.plotly 中是否有功能可以做到这一点,也许是通过 HTML 编码?

 ticktext = ['<font color="red">{}</font>'.format(x) for x in ticktexts]

不起作用,它将 html 字符串提供给标签.

解决方案

使用 LaTeX 的一点解决方法可以帮助你(对不起@Iwileczek,我偷了你的例子,希望你不要'不介意)因为 plotly 有完整的 LaTeX


2021 年 3 月更新:

如果您不想在输出中使用 MathJax 渲染的 LaTex 字体,请使用带有 HTML 样式的@Dapcer 的解决方案:

def color(color, text):return 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") [...]:

这篇关于如何在情节中为ticktext着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)