Python 如何在Matplotlib中换行文本
我想在matplotlib(V3.3.0)中自动换行文本。因此,我从文档中查看了这段代码: 然而,当我运行这段代码时,我得到的输出显然不是我想要的: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
我不知道发生了什么。非常感谢您的帮助。您可以使用
\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
也可以工作。