我想在我的matplotlib图中使用LaTex-signs。我先用过。
pl.ylabel(r’$ \ pi \ rho $’,family =‘Courier New’)
那没关系。但是现在我想要使用的标签是可变的,来自其他* .py fil
实际上发生的事情与字符串格式化无关(即调用 x = "blah %s" % YLabel )。
x = "blah %s" % YLabel
这是由于你最初定义的方式 YLabel 。
YLabel
例如,尝试做:
x = "\rho" print x
该 "\r" 被解释为回车并且不打印。它只是打印“ho”。 (这适用于 许多其他逃逸序列,例如 \n , \t , \x , \f , \b 等 )
"\r"
\n
\t
\x
\f
\b
要避免这种情况,您需要将原始字符串定义为“原始”字符串:
x = r"\rho" print x
或明确逃脱 "\r" 序列:
x = "\\rho" print x