2014-06-11 83 views
4

我正在嘗試使x標籤着色,但我不知道如何確定。難點是我想使用LaTeX字體和包「amsmath」。地塊中的彩色乳膠標籤

我的代碼是:

import math 
import numpy as np 
import matplotlib.pyplot as plt 


x,y=np.random.rand(20),np.random.rand(20) 


plt.figure() 

plt.rc('text', usetex=True) 
plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"] 


plt.plot(x,y,'.') 




plt.xlabel(r'$x=\frac{\text{red}}{\text{blue}}$') 
plt.ylabel(r'$y$') 

#plt.savefig('colored_labels.png') 

plt.show() 

enter image description here

沒有人有一招如何使在明顯的顏色x軸?

預先感謝您!

+0

你想讓整件東西染上一些顏色,還是想讓「紅」紅和「藍」藍? – Ffisegydd

+0

我想要「紅色」紅色和「藍色」藍色 – Andy

+3

請查看:[question](http://stackoverflow.com/questions/9169052/partial-coloring-of-text-in-matplotlib) –

回答

1

我相信這裏的問題可能在Latex政權。 您的命令\text是一個Latex命令,用於在等式中設置文本。它不提供任何顏色。

無需嘗試過自己在Python,我建議你嘗試在乳膠裝xcolor還有:

plt.rcParams['text.latex.preamble']=[r"\usepackage{xcolor}"] 

然後你就可以使用xcolors' \color -command:

plt.xlabel(r'$x=\frac{ \color{red}{red text} }{ \color{blue}{blue text} }$') 

同樣,在Python未經測試,但你甚至可以使用在此之上的\text -command得到更好的結果:

plt.xlabel(r'$x=\frac{ \text{\color{red}{red text}} }{ \text{\color{blue}{blue text}} }$') 

至少該代碼的Latex部分起作用,併爲您提供期望的結果。

讓我知道它是否適合你!

+0

謝謝,但它仍然沒有着色...似乎python不兼容彩色乳膠字體?! – Andy

+0

看起來這樣,是的。基於快速谷歌搜索,我認爲這取決於後端使用,它可能會在ps輸出中獲取彩色字體。但是我失去了這些信息的來源。 – Dux