我想将粗体应用于 matplotlib 中文本中的
some words with space
示例代码
我需要在图表中插入一些文本。
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([1, 2, 3])
s.plot()
plt.text(1.2, 1.8, "Jame's Steward: math exam")
plt.text(1.0, 1.5, "Jame's roy Steward: math exam", weight='bold')
plt.show()
如何仅将冒号 (:) 之前的单词加粗?
这是我在 stackoverflow 上找到的答案。
s = pd.Series([1, 2, 3])
s.plot()
plt.text(1.2, 1.8, r"$\bf{Jame's Steward}$: math exam")
plt.text(1.0, 1.5, r"$\bf{Jame's roy Steward}$: math exam")
plt.show()
此方法无法识别空格,并且字体似乎与粗体有细微的不同。
我可以容忍稍微不同的字体,但请告诉我当空格键只有一部分是粗体时如何识别空格键。
您正在使用 LaTeX 的数学模式 (
$...$
),它将去除空格。
而是将
\textbf{}
与 usetex=True
一起使用(请参阅 text
):
plt.text(1.2, 1.8, r"\textbf{Jame's Steward}: math exam", usetex=True)
plt.text(1.0, 1.5, r"\textbf{Jame's roy Steward}: math exam", usetex=True)
输出: