matplotlib 中的文本对象无法正确响应缩放 [英] Text object in matplotlib doesn't respond to zooming properly

查看:27
本文介绍了matplotlib 中的文本对象无法正确响应缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.大家好.

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

 将matplotlib导入为mplfig=plt.figure()ax1 = fig.add_subplot(111)ax1.text('','', '',position=[0.5,0.5], text='Y', fontsize='xx-small' )

感谢任何帮助.谢谢〜

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

感谢 tcaswell 的建议.TextPath确实达到了我的部分目的.

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

from matplotlib.textpath import TextPath导入matplotlib.pyplot作为plt导入matplotlib.patches作为补丁从 matplotlib.path 导入路径fig=plt.figure()ax1=fig.add_subplot(111)tp1=TextPath((0.5,0.5), r'你如何打开这个?', size=1)多边形=tp1.to_polygons()对于 in 多边形:p1 = patches.Polygon(a)ax1.add_patch(p1)

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

解决方案

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

. Hello, everyone.

I recently try to add text object in my plot. But when I zoom into the text, the text size remains unchanged. What I want to have is that the text size will increase when I zoom in and will decrease when I zoom out.

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

Any help is appreciated. Thanks~

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

Thanks for the suggestion from tcaswell. TextPath does achieve part of my purposes.

I found there is no documentation about textpath in official matplotlib website, so I view the source code to see what it does. Finally, I achieved a not excellent but satisfactory result as follows.

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)

The not excellent part of this code is that it doesn't support rotation and export the text as filled polygon. Is there any easy way to rotate the text? Can I export the text as non-filled polygon?

解决方案

When you create a Polygon instance, you can specify many keyword arguments, including setting fill = False (see details here):

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()

这篇关于matplotlib 中的文本对象无法正确响应缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆