python - 更改 matplotlib 中内联的文本样式

标签 python matplotlib

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

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

最佳答案

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

示例:

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

关于python - 更改 matplotlib 中内联的文本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30890831/

相关文章:

python - Django : Passing api's json to template for use in a table

python - 在图表下设置颜色图

python - 如何将 matplotlib 生成的多个图发送到 pptx 而没有任何重叠?

Python 3 print() 到变量

python - python可以控制itunes之类的应用吗?

python - 与 python 方法中的 self 参数混淆(不属于任何类的方法)

python - Pandas 拆分并选择第二个元素

Python散点图: how to use a colormap that has the same colors as the colorcycle

python - 删除 matplotlib 中子图上的重叠刻度线

python - 步骤绘制样式 plotly 破折号