1

I would like to create a figure in matplotlib that contains some rather long tick-labels. To fit the labels in reasonable space, I've tried using the wrapping provided in matplotlib:

plt.yticks([1],[a],wrap=True)

This works(output), however the tick-label text then extends to the edge of the figure. How can a margin be placed between the figure edge and the tick-label text while the text is set to wrap?

Failed Attempt:

I tried to create the desired margin by expanding the bounding box(output):

f.savefig("stillnomargin.pdf",bbox_inches=f.bbox_inches.expanded(2,2))

However, the wrapping changed to redistribute the text to the new figure edge.

Full Example:

import matplotlib.pyplot as plt

labeltext = "This is a really long string that I'd rather have wrapped so that it"\
    " doesn't go outside of the figure, but if it's long enough it will go"\
    " off the top or bottom!"

a4_width =  8.27
a4_height = 11.69 

f = plt.figure()
plt.ylim([0,2])
plt.xlim([0,2])
plt.yticks([1],[labeltext],wrap=True)

f.set_size_inches(a4_width, a4_height)
plt.subplots_adjust(left=0.3) #leave space on left for large yticklabel


#  outputs full page, yticklabel text to edge
f.savefig("nomargin.pdf")

#  tried to expand bounding box to create margin, 
#  but yticklabel then rewraps to fills space to edge
f.savefig("stillnomargin.pdf",bbox_inches=f.bbox_inches.expanded(2,2))
Gurupad Hegde
  • 2,155
  • 15
  • 30
  • The `wrap=True` option is bound to the figure edge. Moving the figure edge moves the edge for wrapping. It originates from [this question](http://stackoverflow.com/questions/4018860/text-box-with-line-wrapping-in-matplotlib), which might be useful when implementing a custom wrapper, which is what you'd need to do in this case. – ImportanceOfBeingErnest Mar 31 '17 at 07:27

0 Answers0