0

How can I give the extents for a text in Matplotlib, while still wrapping it if needed?

I can make the text wrap at extent of the whole figure like this:

    fig.text(x, y,
             "Hello, I'm a quite long text in a quite small figure, and I will probably be wrapped somewhere along the way!", 
             wrap=True)

And I can use clip_box to clip the text like this:

    fig.text(x, y,
             "Hello, I'm a quite long text. Much of me will be clipped off, when I reach past max_width", 
             wrap=True,
             clip_box=Bbox([[x, y], [max_width, max_height]]) )

But I would like to be able to set the width at which to wrap the text. Is that possible?


edit: as @tom points our in the comments, the behavior here is hardcoded: https://github.com/matplotlib/matplotlib/blob/65158f9351ffdb2f9b77ac98277592b58c28ee2a/lib/matplotlib/text.py#L600
Any suggestions for a workaround are very welcome!

leo
  • 8,106
  • 7
  • 48
  • 80
  • 1
    looks like the `wrap=True` mechanics are hardwired to use the figure extents, so looks like there's no built-in solution to this – tmdavison Jul 05 '18 at 10:50
  • 1
    You are for sure not the first one with this problem. Did you look for other questions like this? There should be an answer around which uses the `textwrap` module. – ImportanceOfBeingErnest Jul 05 '18 at 14:56
  • For future reference, [this Q&A](https://stackoverflow.com/questions/8802918/my-matplotlib-title-gets-cropped) provides some options. – ImportanceOfBeingErnest Jul 06 '18 at 10:50
  • @ImportanceOfBeingErnest Thanks. I ended up just temporarily resizing the figure, to take advantage of the built in wrapping-logic. No side-effects so far, so at least in my case that seems to work – leo Jul 06 '18 at 13:19

0 Answers0