将换行符与Python中的TeX一起放在matplotlib标签中? [英] Putting newline in matplotlib label with TeX in Python?
本文介绍了将换行符与Python中的TeX一起放在matplotlib标签中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在matplotlib中向图的标签(例如xlabel或ylabel)添加换行符?例如,
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")
理想情况下,我也希望y标签也居中.有没有办法做到这一点?标签必须同时包含TeX(用'$'括起来)和换行符.
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.
推荐答案
您的示例正是使用\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
这篇关于将换行符与Python中的TeX一起放在matplotlib标签中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文