Automatic subplot_params for fitting label text (was Re: [Matplotlib-users] Suggestions for auto scaling fonts ,

etc for producing images at various scales)
Reply-To:
In-Reply-To: <20110121232036.GA26739@...905...>
X-PGP-Key: http://pirsquared.org/PaulIvanov0F3E28F7.asc

Paul Ivanov, on 2011-01-21 15:20, wrote:

I'm almost certain that one *can* write a function to do this
pro grammatically (without having to hand tweak anything), by
looking at say, the .get_window_extent() but I haven't found the
time to scratch that itch, yet.

If someone wants to beat me to it, here's the sort of thing that
you can do:

  def show_labels_by_shrinking(ax):
    " adjust subplot parameters to fit the yaxis label"
    f = ax.figure
    textwidth = ax.yaxis.get_label().get_window_extent().width
    labelwidth = max([lab.get_window_extent().width for lab in ax.get_yticklabels()])
    plt.subplots_adjust(left=(textwidth+labelwidth+3*ax.yaxis.labelpad)/f.get_window_extent().width)
    # the 3 *ax.yaxis.labelpad is just a fudge factor for now, need
    # to look into the sourcecode to figure out what the
    # appropriate placement is normally, to know how to adjust
    # properly
  
  ax.set_ylabel('foo')
  show_labels_by_shrinking(ax)
  ax.set_ylabel("a\nmulti\nline\nexample")
  show_labels_by_shrinking(ax)

I should add that along with doing a similar thing for the xaxis,
this would need to be extended for the multiple subplot case, to
also adjust the wspace and hspace parameters accordingly.

this is important functionality currently missing from matplotlib
out-of-the-box at the moment, so I'll try my crack at it this out
this weekend.

CCing the devel list in case someone has more opinions.

best,

ยทยทยท

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7