2012-02-06 64 views
31

matplotlib中有一种方法可以部分指定字符串的颜色吗?matplotlib中文本的部分着色

例子:

plt.ylabel("Today is cloudy.") 

我怎样才能显示 “今天” 红 “是” 绿色和 “龙龙”。如蓝色?

谢谢。

+2

我想你将不得不用3个独立的文本框来破解它。 – wim 2012-02-07 00:40:54

+0

询问matplotlib邮件列表。也许可以使用自定义渲染器或“艺术家”。 – 2012-02-07 09:58:12

+1

也在https://github.com/matplotlib/matplotlib/issues/697 – 2012-02-07 18:45:21

回答

18

我只知道如何以非交互方式做到这一点,即使这样只有'PS'后端。

为此,我将使用Latex格式化文本。然后,我会包含'颜色'包,并根据需要设置颜色。

下面是这样的一个例子:

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转换,这样我就可以在这里发布): enter image description here

+1

我会使用这个,如果只是它与PDF后端工作:)出于某种原因,我永远无法正确放置轴画布,而我正在与ps后端工作。 – 2012-02-08 17:03:10

+0

对不起 - 我不是故意低估这一点。我的意思是让它升级,我必须早点误会。 – mixedmath 2014-09-09 23:13:59

+0

非常好的解决方案。有没有办法创建'pdf'?除了将它保存为'ps',然后是'ps2pdf',这几乎把我的图表中的所有内容搞砸了。 – 2015-09-17 22:32:32

18

这里的互动版(同一个我张贴到the list

import matplotlib.pyplot as plt 
from matplotlib import transforms 

def rainbow_text(x,y,ls,lc,**kw): 
    """ 
    Take a list of strings ``ls`` and colors ``lc`` and place them next to each 
    other, with text ls[i] being shown in color lc[i]. 

    This example shows how to do both vertical and horizontal text, and will 
    pass all keyword arguments to plt.text, so you can set the font size, 
    family, etc. 
    """ 
    t = plt.gca().transData 
    fig = plt.gcf() 
    plt.show() 

    #horizontal version 
    for s,c in zip(ls,lc): 
     text = plt.text(x,y," "+s+" ",color=c, transform=t, **kw) 
     text.draw(fig.canvas.get_renderer()) 
     ex = text.get_window_extent() 
     t = transforms.offset_copy(text._transform, x=ex.width, units='dots') 

    #vertical version 
    for s,c in zip(ls,lc): 
     text = plt.text(x,y," "+s+" ",color=c, transform=t, 
       rotation=90,va='bottom',ha='center',**kw) 
     text.draw(fig.canvas.get_renderer()) 
     ex = text.get_window_extent() 
     t = transforms.offset_copy(text._transform, y=ex.height, units='dots') 


plt.figure() 
rainbow_text(0.5,0.5,"all unicorns poop rainbows ! ! !".split(), 
     ['red', 'orange', 'brown', 'green', 'blue', 'purple', 'black'], 
     size=40) 

all unicorns poop rainbows

+0

看起来这些词在垂直版本中并不完全一致。 – Alex 2016-10-12 19:37:04

+1

这实际上是我写这篇评论时matplotlib中的一个错误。它已被修复,正如你可以看到[这里](http://matplotlib.org/examples/text_labels_and_annotations/rainbow_text.html)。 – 2016-10-24 23:52:19

4

扩展晏的回答,乳胶着色现在also works with PDF export

import matplotlib 
from matplotlib.backends.backend_pgf import FigureCanvasPgf 
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf) 

import matplotlib.pyplot as plt 

pgf_with_latex = { 
    "text.usetex": True,   # use LaTeX to write all text 
    "pgf.rcfonts": False,   # Ignore Matplotlibrc 
    "pgf.preamble": [ 
     r'\usepackage{color}'  # xcolor for colours 
    ] 
} 
matplotlib.rcParams.update(pgf_with_latex) 

plt.figure() 
plt.ylabel(r'\textcolor{red}{Today} '+ 
      r'\textcolor{green}{is} '+ 
      r'\textcolor{blue}{cloudy.}') 
plt.savefig("test.pdf") 

注意这个python脚本有时会失败,在第一次尝试Undefined control sequence错误。再次运行它是成功的。