对 matplotlib 文本中带有空格键的某些单词应用粗体

问题描述 投票:0回答:1

我想将粗体应用于 matplotlib 中文本中的

some words with spacebar

示例代码

我需要在图表中插入一些文本。

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()

enter image description here

如何仅将冒号 (:) 之前的单词加粗?

这是我在 stackoverflow 上找到的答案。

使用 matplotlib 将某些文本显示为粗体

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()

enter image description here

此方法无法识别空格键,并且字体似乎与粗体略有不同。

我可以容忍稍微不同的字体,但请告诉我当空格键只有一部分是粗体时如何识别空格键。

python matplotlib
1个回答
0
投票

您正在使用 LaTeX 的数学模式 (

$...$
),它将去除空格。

而是将

\textbf{}
usetex=True
一起使用:

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)

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.