在matplotlib中更改内联文本样式

时间:2015-06-17 11:54:00

标签: python matplotlib

我需要使用ax.textfig.text在图中添加两个单词。问题是第一个单词应该用粗体和斜体书写,第二个单词没有任何特定的样式。例如

一个 两个

这两个词应该写成同一个句子。我可以使用两次调用ax.text,但我想避免手动设置两个标签之间的对齐方式。最好的方法是什么?

1 个答案:

答案 0 :(得分:3)

Matplotlib允许您使用乳胶。默认情况下不启用它,因此您必须先激活它。

示例:

import matplotlib.pyplot as plt

# activate usage of latex
plt.rcParams['text.usetex'] = True

# generate a simple graph
fig, ax = plt.subplots(1,1, figsize=(6,3))
ax.text(0.5, 0.5, r"\textit{\textbf{one}} two (on the axes)")
fig.text(1., 1., r"\textit{\textbf{one}} two (on the figure)",
        horizontalalignment='right', verticalalignment='top')

plt.show()

结果:

enter image description here

相关问题