matplotlib使用非ascii和LaTeX char在两行上拆分标题

时间:2017-07-04 17:48:44

标签: python string matplotlib

我需要使用matplotlib.pyplot将一个数字标题拆分为两行,其中包含非ascii字符。

这是我迄今为止所做的尝试:

plt.title(u"Some very long string with non ascii chara éèàéüöëêâûô\n"
"and the next line should be here, with maybe some LaTeX symbols like greek letters in addition éèàéüöëêâûô")

plt.title(r"Some very long string with non ascii chara éèàéüöëêâûô\n"
"and the next line should be here, with maybe some LaTeX symbols like greek letters in addition éèàéüöëêâûô")

两个声明版本都给我一些错误。

3 个答案:

答案 0 :(得分:1)

只是不要以"

结束这一行
plt.title(u"Some very long string with non ascii chara éèàéüöëêâûô\n and the next line should be here, with maybe some LaTeX symbols like greek letters in addition éèàéüöëêâûô")

或尝试:

plt.title(u"""Some very long string with non ascii chara éèàéüöëêâûô
and the next line should be here, with maybe some LaTeX symbols like greek letters in addition éèàéüöëêâûô""")

答案 1 :(得分:1)

好的,我终于找到了唯一可行的方法:

感谢Dimgold的工作解决方案,但只有在没有LaTeX字符时才有效。

当您需要LaTeX char时,您必须先使用' r '标记排版字符串。
然后,换行符'\ n'将不再起作用。

您必须从__future__导入一些额外内容。

以下是我的有效解决方案:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
from __future__ import unicode_literals # It seems absolutely needed.

# some stuff to plot here

plt.title(r"""Some very long string with non ascii chara éèàéüöëêâûô
and the next line should be here, with maybe some LaTeX symbols like $\alpha$ or $\overrightarrow{vector}$ in addition éèàéüöëêâûô""")

答案 2 :(得分:-1)

好的老cragige return chr(10) 仍然适用于我们这些老家伙

xx= "Freds Plots "



  axs[1,1]       or whatever
    .set_title(xx + chr(10) + 'y=10 -2x y=2x+5, y=3x-10' + chr(10) + 'y=x  y=0 x=0

')