Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Matplotlib中换行文本_Python_Matplotlib_Plot - Fatal编程技术网

Python 如何在Matplotlib中换行文本

Python 如何在Matplotlib中换行文本,python,matplotlib,plot,Python,Matplotlib,Plot,我想在matplotlib(V3.3.0)中自动换行文本。因此,我从文档中查看了这段代码: 然而,当我运行这段代码时,我得到的输出显然不是我想要的: 我不知道发生了什么。非常感谢您的帮助。您可以使用\n来指定将文本分成多行的位置: import matplotlib.pyplot as plt fig = plt.figure() plt.axis([0, 10, 0, 10]) t = ("This is a really long string that I'd rather

我想在matplotlib(V3.3.0)中自动换行文本。因此,我从文档中查看了这段代码:

然而,当我运行这段代码时,我得到的输出显然不是我想要的:


我不知道发生了什么。非常感谢您的帮助。

您可以使用
\n
来指定将文本分成多行的位置:

import matplotlib.pyplot as plt

fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = ("This is a really long string that I'd rather have wrapped so that it \n"
 "doesn't go outside of the figure, but if it's long enough it will go \n"
 "off the top or bottom!")
plt.text(4, 1, t, ha='left', rotation=15)
plt.text(6, 5, t, ha='left', rotation=15)
plt.text(5, 5, t, ha='right', rotation=-15)
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
     va='top')
plt.text(3, 4, t, family='serif', style='italic', ha='right')
plt.text(-1, 0, t, ha='left', rotation=-15)

plt.show()

您可以使用
\n
指定要将文本分成多行的位置:

import matplotlib.pyplot as plt

fig = plt.figure()
plt.axis([0, 10, 0, 10])
t = ("This is a really long string that I'd rather have wrapped so that it \n"
 "doesn't go outside of the figure, but if it's long enough it will go \n"
 "off the top or bottom!")
plt.text(4, 1, t, ha='left', rotation=15)
plt.text(6, 5, t, ha='left', rotation=15)
plt.text(5, 5, t, ha='right', rotation=-15)
plt.text(5, 10, t, fontsize=18, style='oblique', ha='center',
     va='top')
plt.text(3, 4, t, family='serif', style='italic', ha='right')
plt.text(-1, 0, t, ha='left', rotation=-15)

plt.show()

很抱歉,我无法提供更多帮助,但我使用已安装的版本进行了测试,
3.2.2
,它的工作原理与示例中的一样,我使用3.6获取未包装的文本。10@ponir有点长,但您能否尝试在示例的顶部添加
import matplotlib
,然后将
t
变量更改为
t=matplotlib.\uu版本\uuu
?@tomjn我不确定这能实现什么。链接到图像:抱歉,我不能更多帮助,但我使用我安装的版本进行了测试,
3.2.2
,它的工作原理与示例中的一样,我使用3.6获取未包装的文本。10@ponir有点长,但您能否尝试在示例的顶部添加
import matplotlib
,然后将
t
变量更改为
t=matplotlib.\uu版本\uuu
?@tomjn我不确定这会实现什么。链接到图像:这不是吗放弃使用wrap参数的意义?完全可以,但这是我发现的将文本分成多行的唯一方法!事实上,使用
\n
即使不设置
wrap=True
,也能工作。这难道不违背使用wrap参数的意义吗?确实如此,但这是我发现的将文本分成多行的唯一方法!事实上,即使没有设置
wrap=True
,使用
\n
也可以工作。