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

6 投票
2 回答
2019 浏览
提问于 2025-04-18 04:19

大家好。

我最近尝试在我的图表中添加文本对象。但是当我放大文本时,文本的大小却没有变化。我希望的是,当我放大时,文本的大小会增大,而当我缩小时,文本的大小会减小。

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

感谢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)

这个代码不完美的地方在于,它不支持旋转,也不能将文本导出为填充的多边形。有没有简单的方法可以旋转文本?我可以将文本导出为非填充的多边形吗?

2 个回答

2

感谢你的提问和之前的准备工作!我的解决方案只需要通过以下命令来调用:

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

当你创建一个多边形的实例时,可以指定很多关键词参数,其中包括设置 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()

figure image

撰写回答