繁体   English   中英

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

[英]Text object in matplotlib doesn't respond to zooming properly

. 大家好。

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

import matplotlib as mpl
fig=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
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)

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

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

人物形象

感谢您的提问和准备工作! 我的解决方案只是由命令调用:

text_fixed_size(ax=ax, text=f'pos=(4,8), h=1', pos=(4,8), h=1, color=c)

并在以下(实心文本和孔,如“o”中,未填充)图片中产生橙色输出。 此解决方案也适用于倒置轴。 如果需要,可以独立设置高度和宽度。

在此处输入图片说明

代码

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

def text_fixed_size(ax, text, pos, w=None, h=None, auto_trans=True, color='k'):

    assert not (w is None and h is None)

    tp = mpl.textpath.TextPath((0.0,0.0), text, size=1)
    x0, y0 = np.amin(np.array(tp.vertices), axis=0)
    x1, y1 = np.amax(np.array(tp.vertices), axis=0)
    hax = -np.subtract(*ax.get_xlim())
    wax = -np.subtract(*ax.get_ylim())

    if w is None:
        w = h / (y1 - y0) * ( x1 - x0)
    if h is None:
        h = w / (x1 - x0) * ( y1 - y0)
    if auto_trans:
        w *= np.sign(hax)
        h *= np.sign(wax)

    verts = []
    for vert in tp.vertices:
        vx = vert[0] * w / (x1 - x0) + pos[0]
        vy = vert[1] * h / (y1 - y0) + pos[1]
        verts += [ [vx, vy] ]
    verts = np.array(verts)

    tp = mpl.path.Path(verts, tp.codes)
    ax.add_patch(mpl.patches.PathPatch(tp, facecolor=color, lw=0))

fig, axs = plt.subplots(2, 2)
axs = np.array(axs).T.flatten()

lims = np.array([[0, 15], [0,10]])
for aa, ax in enumerate(axs):
    d0 = int((-(aa%2)+.5)*2)
    d1 = int((-(aa//2)+.5)*2)
    l = np.array([lims[0, ::d0], lims[1, ::d1]])
    ax.set_xlim(l[0, 0], l[0, 1])
    ax.set_ylim(l[1, 0], l[1, 1])

for aa, ax in enumerate(axs):
    c = 'C0'
    text_fixed_size(ax=ax, text=f'pos=(6,3), w=5, h=1',
                    pos=(6,3), w=5, h=1, color=c)
    c = 'C1'
    text_fixed_size(ax=ax, text=f'pos=(4,8), h=1',
                    pos=(4,8), h=1, color=c)
    c = 'C2'
    text_fixed_size(ax=ax, text=f'pos=(8,1), w=5, auto_trans=False',
                    pos=(3,1), w=10, auto_trans=False, color=c)

plt.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM