简体   繁体   English

在matplotlib图上裁剪文本

[英]Cropping text on matplotlib plot

I am making a plot and I want the text to crop at the edge. 我正在制作一个情节,我希望文本在边缘裁剪。 Right now it hangs over the edge, which is great for readability, but not what I actually want. 现在它挂在边缘,这对于可读性来说非常好,但不是我真正想要的。

Here's a toy version of what I'm doing: 这是我正在做的玩具版本:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

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

ax.scatter(np.random.random(10), np.random.random(10))
ax.text(0.8, 0.5, "a rather long string")

plt.show()

我的matplotlib示例

Just to be clear, I want to crop my text element, but not anything else — eg I want to leave the 0.9 in the x -axis alone. 为了清楚起见,我想裁剪我的text元素,但不是其他任何东西 - 例如,我想在x轴中单独留下0.9

You should set a clipbox for the text as described in the documentation : 您应该按照文档中的说明为文本设置剪贴框:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

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

ax.scatter(np.random.random(10), np.random.random(10))
ax.text(0.8, 0.5, "a rather long string",
        clip_box=ax.clipbox, clip_on=True)
ax.set_xlim(0, 1)

plt.show()

This results in 这导致了

在此输入图像描述

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

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