Reputation: 8152
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()
Just to be clear, I want to crop my text
element, but not anything else — e.g. I want to leave the 0.9
in the x-axis alone.
Upvotes: 3
Views: 769
Reputation: 24328
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
Upvotes: 4