You are reading an old version of the documentation (v2.1.2). For the latest version see
Version 2.1.2
Fork me on GitHub


Related Topics

This Page

Auto Subplots AdjustΒΆ

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

def on_draw(event):
   bboxes = []
   for label in labels:
       bbox = label.get_window_extent()
       # the figure transform goes from relative coords->pixels and we
       # want the inverse of that
       bboxi = bbox.inverse_transformed(fig.transFigure)

   # this is the bbox that bounds all the bboxes, again in relative
   # figure coords
   bbox = mtransforms.Bbox.union(bboxes)
   if fig.subplotpars.left < bbox.width:
       # we need to move it over
       fig.subplots_adjust(left=1.1*bbox.width) # pad a little
   return False

fig.canvas.mpl_connect('draw_event', on_draw)

Total running time of the script: ( 0 minutes 0.020 seconds)

Gallery generated by Sphinx-Gallery