在Python中使用TeX在matplotlib标签中添加换行符?

发布于 2024-08-29 14:46:58 字数 268 浏览 6 评论 0原文

如何在 matplotlib 中向绘图标签(例如 xlabel 或 ylabel)添加换行符?例如,

plt.bar([1, 2], [4, 5])
plt.xlabel("My x label")
plt.ylabel(r"My long label with $\Sigma_{C}$ math \n continues here") 

理想情况下,我希望 y 标签也居中。有办法做到这一点吗?重要的是标签同时包含 TeX(包含在“$”中)和换行符。

How can I add a newline to a plot's label (e.g. xlabel or ylabel) in matplotlib? For example,

plt.bar([1, 2], [4, 5])
plt.xlabel("My x label")
plt.ylabel(r"My long label with $\Sigma_{C}$ math \n continues here") 

Ideally I'd like the y-labeled to be centered too. Is there a way to do this? It's important that the label have both TeX (enclosed in '$') and the newline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉生梦死 2024-09-05 14:46:58

您可以两全其美:自动“转义”LaTeX 命令换行符:(

plt.ylabel(r"My long label with unescaped {\LaTeX} $\Sigma_{C}$ math"
           "\n"  # Newline: the backslash is interpreted as usual
           r"continues here with $\pi$")

不是使用三行,而是用单个空格分隔字符串是另一种选择)。

事实上,Python 会自动连接彼此跟随的字符串文字,您可以混合原始字符串 (r"…") 和带有字符插值的字符串 ("\n") 。

You can have the best of both worlds: automatic "escaping" of LaTeX commands and newlines:

plt.ylabel(r"My long label with unescaped {\LaTeX} $\Sigma_{C}$ math"
           "\n"  # Newline: the backslash is interpreted as usual
           r"continues here with $\pi$")

(instead of using three lines, separating the strings by single spaces is another option).

In fact, Python automatically concatenates string literals that follow each other, and you can mix raw strings (r"…") and strings with character interpolation ("\n").

青衫儰鉨ミ守葔 2024-09-05 14:46:58

您的示例正是如何完成的,您使用 \n。不过,您需要去掉 r 前缀,这样 python 就不会将其视为原始字符串

Your example is exactly how it's done, you use \n. You need to take off the r prefix though so python doesn't treat it as a raw string

楠木可依 2024-09-05 14:46:58
plt.bar([1, 2], [4, 5])
plt.xlabel("My x label")
plt.ylabel(r"My long label with $\Sigma_{C}$ math" + "\n" + "continues here")

只需将字符串与非原始字符串形式的换行符连接起来即可。

plt.bar([1, 2], [4, 5])
plt.xlabel("My x label")
plt.ylabel(r"My long label with $\Sigma_{C}$ math" + "\n" + "continues here")

Just concatenate the strings with a newline that isn't in raw string form.

另类 2024-09-05 14:46:58

以下 matplotlib python 脚本创建带有换行符的文本

ax.text(10, 70, 'shock size \n $n-n_{fd}

以下内容没有换行符。注意文本前面的 r

ax.text(10, 70, r'shock size \n $n-n_{fd}
)

以下内容没有换行符。注意文本前面的 r


)
)

以下内容没有换行符。注意文本前面的 r

The following matplotlib python script creates text with new line

ax.text(10, 70, 'shock size \n $n-n_{fd}

The following does not have new line. Notice the r before the text

ax.text(10, 70, r'shock size \n $n-n_{fd}
)

The following does not have new line. Notice the r before the text


)
)

The following does not have new line. Notice the r before the text

泛滥成性 2024-09-05 14:46:58

如果有人想要 TeX(例如部分粗体文本)和一个新行并且其中有一个百分号(我挣扎的时间比我想要的要长):

import matplotlib.pyplot as plt

plt.plot([10, 20], [10, 20])  # dummy plot
value = 20  # dummy value

bold_text_base = f"Value = %u\ \%%"  # "\ " for protected space, "\%%" for percentage sign that survives formatting and math mode
regular_text = "(2nd line here)"
bold_text = bold_text_base % value
_ = plt.ylabel(r"$\bf{{{x}}}$".format(x=bold_text) + f"\n%s" % regular_text )  # suppress output with "_ = "

返回:

Matplotlib 绘图,标题中带有 TeX、换行符和百分号

In case anyone wants TeX (e.g. for partly bold text) and a new line AND has a percentage sign in it (I struggled longer than I wanted):

import matplotlib.pyplot as plt

plt.plot([10, 20], [10, 20])  # dummy plot
value = 20  # dummy value

bold_text_base = f"Value = %u\ \%%"  # "\ " for protected space, "\%%" for percentage sign that survives formatting and math mode
regular_text = "(2nd line here)"
bold_text = bold_text_base % value
_ = plt.ylabel(r"$\bf{{{x}}}
quot;.format(x=bold_text) + f"\n%s" % regular_text )  # suppress output with "_ = "

Returns:

Matplotlib plot with TeX, newline and percentage sign in title

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文