matplotlib 中的文本对象无法正确响应缩放

Ken*_*n T 6 python matplotlib

. 大家好。

我最近尝试在我的情节中添加文本对象。但是当我放大文本时,文本大小保持不变。我想要的是放大时文本大小会增加,缩小时会减小。

import matplotlib as mpl
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.text('','', '',position=[0.5,0.5], text='Y', fontsize='xx-small' )
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。谢谢~

补充-UTC+8 30/04/2013 9:40 AM

感谢 tcaswell 的建议。TextPath 确实实现了我的部分目的。

我发现 matplotlib 官方网站上没有关于 textpath 的文档,所以我查看源代码以了解它的作用。最后,我得到了一个不出色但令人满意的结果,如下所示。

from matplotlib.textpath import TextPath
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Path

fig=plt.figure()
ax1=fig.add_subplot(111)
tp1=TextPath((0.5,0.5), r'How do you turn this on?', size=1)
polygon=tp1.to_polygons()
for a in polygon:
    p1=patches.Polygon(a)
    ax1.add_patch(p1)
Run Code Online (Sandbox Code Playgroud)

这段代码不太好的部分是它不支持旋转并将文本导出为填充多边形。有没有简单的方法来旋转文本?我可以将文本导出为非填充多边形吗?

小智 2

创建 Polygon 实例时,您可以指定许多关键字参数,包括设置fill = False(请参阅此处的详细信息):

from matplotlib.textpath import TextPath
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.path import Path

fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.set_ylim(-1 , 3)
ax1.set_xlim(-3, 15)
tp1=TextPath((0.0,0.5), r'How do you turn this on?', size=1)
polygon=tp1.to_polygons()
for a in polygon:
    p1=patches.Polygon(a, fill=False)
    ax1.add_patch(p1)

plt.show()
Run Code Online (Sandbox Code Playgroud)

图形图像