我需要在两个数字中使用ax.text
或fig.text
。问题在于,第一个单词应以粗体和斜体书写,第二个单词应没有任何特殊样式。例如
一二
这两个词应写成好像在同一句子中一样。我可以对ax.text
使用两次调用,但是我想避免手动设置两个标签之间的对齐方式。最好的方法是什么?
最佳答案
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()
结果:
关于python - 在matplotlib中内联更改文本样式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30890831/