matplotlib 文本未剪裁 [英] matplotlib text not clipped

查看:33
本文介绍了matplotlib 文本未剪裁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 text() matplotlib 中绘制文本,然后以交互方式平移图像时,生成的绘制文本不会剪切到数据窗口.这与使用 annotate()绘制数据或绘制文本的工作方式相反,并且由于 text()使用数据窗口坐标而没有直观意义.

When drawing text in matplotlib with text(), and then interactively panning the image, the resulting drawn text is not clipped to the data window. This is counter to how plotting data or drawing text using annotate() works, and doesn't make intuitive sense as text() uses data window coordinates.

import matplotlib.pyplot as plt

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)

ax.text(0.5, 0.2, 'text')
ax.annotate('anno', (0.5, 0.3))

plt.draw()

交互式地将文本从所有窗口移出数据窗口.当参考点穿过数据窗口边界时,annotate() 绘制的 'anno' 被裁剪,而 text() 绘制的 'text' 没有.

Interactively pan the text out of the data window on all sides. The annotate() drawn 'anno' is clipped when the reference point crosses the data window boundary, while the text() drawn 'text' is not.

我不确定这种行为是功能还是错误,但肯定是后者,因为此文本会干扰轴标签等.使用 1.2.1 和 TkAgg 后端.

I'm not sure if this behavior a feature or a bug, but sure seems like the latter, as this text interferes with axis labels, etc. Using 1.2.1 with TkAgg backend.

另一个问题是如何正确裁剪所有文本,使其不超出数据窗口,而不仅仅是在参考坐标发生时.

An additional question would be how to properly clip all text from going outside the data window, not just when the reference coordinate does.

谢谢!

推荐答案

这个行为可以被 kwarg clip_on 控制:

This behavior can be controled by the kwarg clip_on:

import matplotlib.pyplot as plt

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)

txt = ax.text(0.5, 0.2, 'text')
anno = ax.annotate('anno', (0.5, 0.3))
txt_clip = ax.text(0.5, 0.5, 'text-clip', clip_on=True)

plt.draw()

axes.text doc.有支持和反对将文本剪切到数据区域的论点.

axes.text doc. There are arguments both for and against clipping the text to the data area.

mpl 中有一个 bug 导致 txt.set_clip_on(True) 未按预期工作.

There was a bug in mpl that made txt.set_clip_on(True) not work as expected.

这篇关于matplotlib 文本未剪裁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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