在 matplotlib 中内联更改文本样式 [英] change text style inline in matplotlib

查看:46
本文介绍了在 matplotlib 中内联更改文本样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 ax.text fig.text 在图中添加两个单词.问题在于,第一个单词应以粗体和斜体书写,第二个单词应没有任何特殊样式.例如

I need to put two words in a figure, using ax.text or fig.text. The problem is that the first word should be written in bold and italic, the second without any particular style. For example

一个两个

one two

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

The two words should be written as if they are in the same sentence. I can use two calls to ax.text, but I would like to avoid to set manually the alignment between the two labels. What's the best way to do it?

推荐答案

Matplotlib 允许您使用 Latex.默认情况下未启用该功能,因此您必须先将其激活.

Matplotlib allows you to use latex. It is not enabled by default, so you have to activate it first.

示例:

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

结果:

这篇关于在 matplotlib 中内联更改文本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆