Skip to main content
 首页 » 编程设计

python之latex 颜色未在 matplotlib 文本中呈现

2024年11月24日11birdshome

Tex 的 \textcolor 在我的绘图脚本中似乎被忽略了

import matplotlib as matplotlib 
from matplotlib import pyplot as plt 
 
matplotlib.rcParams.update({'text.usetex': True}) 
 
matplotlib.rc( 
    'text.latex', preamble=r"\usepackage{xcolor}") 
 
fig, ax = plt.subplots() 
ax.set_ylabel(r'\textcolor{red}{aaaaaaa}') 
plt.show() 

不给我红色文本,它产生:

我错过了什么吗?

请您参考如下方法:

这里有更详细的解释:https://matplotlib.org/users/usetex.html但似乎只有当您将其导出到 ps 文件时它才有效。对我来说,如果你将它保存为 ps 文件,它可以使用彩色,而同一文件内联不起作用。

这里有一些解决方法。

import matplotlib as matplotlib 
from matplotlib import pyplot as plt 
 
matplotlib.rcParams.update({'text.usetex': True}) 
matplotlib.rc('text.latex', preamble=r"\usepackage{xcolor}") 
 
fig, ax = plt.subplots() 
ax.set_ylabel(r"aaaaaaa", color='r') 
 
#plt.savefig(r"foo.ps") 
# you can include the above line if you're using your old code. 
 
plt.show()