matplotlib 中文本的部分着色 [英] Partial coloring of text in matplotlib
本文介绍了matplotlib 中文本的部分着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
matplotlib 有没有办法部分指定字符串的颜色?
Is there a way in matplotlib to partially specify the color of a string?
示例:
plt.ylabel("Today is cloudy.")
如何将今天"显示为红色,将是"显示为绿色和阴天".如蓝色?
How can I show "today" as red, "is" as green and "cloudy." as blue?
谢谢.
推荐答案
我只知道如何以非交互方式执行此操作,即使那样也只能使用PS"后端.
I only know how to do this non-interactively, and even then only with the 'PS' backend.
为此,我将使用 Latex 来格式化文本.然后我会包含颜色"包,并根据需要设置颜色.
To do this, I would use Latex to format the text. Then I would include the 'color' package, and set your colors as you wish.
以下是执行此操作的示例:
Here is an example of doing this:
import matplotlib
matplotlib.use('ps')
from matplotlib import rc
rc('text',usetex=True)
rc('text.latex', preamble='usepackage{color}')
import matplotlib.pyplot as plt
plt.figure()
plt.ylabel(r' extcolor{red}{Today} '+
r' extcolor{green}{is} '+
r' extcolor{blue}{cloudy.}')
plt.savefig('test.ps')
这导致(使用 ImageMagick 从 ps 转换为 png,所以我可以在这里发布):
This results in (converted from ps to png using ImageMagick, so I could post it here):
这篇关于matplotlib 中文本的部分着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文