Python matplotlib中的文本对象不';不能正确地响应缩放

Python matplotlib中的文本对象不';不能正确地响应缩放,python,matplotlib,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' ) 感谢您的帮助。谢谢~ 补充UTC+8 2013年4月30日上午9:40 谢谢你的建议。T

。 各位好

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

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 2013年4月30日上午9:40

谢谢你的建议。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)

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

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


谢谢你的提问和准备工作!我的解决方案刚刚被命令调用:

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

库的行为符合预期(文本以相对于图形绝对大小的固定字体大小呈现)。要获取所需内容,请将文本转换为路径并手动打印。我可以通过调整axis.text中的tranform参数来执行此操作吗?此外,还有关于如何将文本转换为路径的演示吗?使用
TextPath
我不知道如何进行包装
TextPath
没有参数
wrap
。有什么想法吗?谢谢!
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()