使matplotlib标题的一部分加粗和不同的颜色

use*_*093 13 python matplotlib

我想将标题的一部分改为粗体.例如:

plt.title("This is title number: " + str(number))
Run Code Online (Sandbox Code Playgroud)

鉴于如上所述的标题,我将如何加粗该str(number)部分.

Imp*_*est 23

从matplotlib版本2开始,不需要使用乳胶(这需要安装胶乳).可以使用普通的MathText以粗体呈现标题的一部分.

import matplotlib.pyplot as plt
number = 2017
plt.title("This is title number: " + r"$\bf{" + str(number) + "}$")
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @Alpha数学模式中的空格用斜杠“ \\”实现。参见例如[this](https://tex.stackexchange.com/questions/375709/space-in-math-mode)。 (4认同)
  • 我喜欢这种设置字体的方式。我们如何使用它设置颜色? (2认同)

use*_*093 12

激活乳胶文本渲染

from matplotlib import rc
rc('text', usetex=True)

plt.title("This is title number: " + r"\textbf{" + str(number) + "}")
Run Code Online (Sandbox Code Playgroud)

  • 如何为标题中的文本设置颜色? (2认同)