2012-02-06 45 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錯誤。再次運行它是成功的。