matplotlib中文本的部分着色matplotlib中有一种方法可以部分指定字符串的颜色吗?例:plt.ylabel("Today is cloudy.")如何将“今天”显示为红色,“是”显示为绿色和“阴天”。如蓝色?谢谢。
3 回答

森栏
TA贡献1810条经验 获得超5个赞
延伸Yann的答案,LaTeX着色现在也适用于PDF导出:
import matplotlibfrom matplotlib.backends.backend_pgf import FigureCanvasPgfmatplotlib.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
在第一次尝试时失败并显示错误。然后再次运行它是成功的。
添加回答
举报
0/150
提交
取消