您可以充分利用这两个方面:自动“转义”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" )。
r"…"
"\n"
plt.bar([1, 2], [4, 5]) plt.xlabel("My x label") plt.ylabel(r"My long label with $\Sigma_{C}$ math" + "\n" + "continues here")
只需将字符串与不是原始字符串形式的换行连接起来。
以下matplotlib python脚本使用换行创建文本
ax.text(10, 70, 'shock size \n $n-n_{fd}$')
以下没有新行。注意文本前面的r
ax.text(10, 70, r'shock size \n $n-n_{fd}$')
你使用的例子就是它的完成方式 \n 。您需要取消r前缀,因此python不会将其视为原始字符串
\n