matplotlib中的文本部分着色 [英] Partial coloring of text in matplotlib

查看:160
本文介绍了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'\textcolor{red}{Today} '+
           r'\textcolor{green}{is} '+
           r'\textcolor{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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆