In the FAQ, there is an entry about adjusting subplot parameters to make room for really long tick labels:

http://matplotlib.sourceforge.net/faq/howto_faq.html#automatically-make-room-for-tick-labels

I made the example a little more general and included a utility function that takes care of tick labels and other objects on all sides of the graph (the example only corrects for overflows on the left). This code might be nice to go into the FAQ. I've attached the code. Please use it if you think it is good.

Thanks,

Jason

--
Jason Grout


import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))
ax.set_yticks((2,5,7))
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

# To test labels on the right side, uncomment below
#ax.yaxis.set_ticks_position('right')

def on_draw(event):
    import matplotlib.transforms as mtransforms
    figure=event.canvas.figure
    bboxes = []
    for ax in figure.axes:
        bbox = ax.xaxis.get_label().get_window_extent()
        # the figure transform goes from relative coords->pixels and we
        # want the inverse of that
        bboxi = bbox.inverse_transformed(figure.transFigure)
        bboxes.append(bboxi)

        bbox = ax.yaxis.get_label().get_window_extent()
        bboxi = bbox.inverse_transformed(figure.transFigure)
        bboxes.append(bboxi)
        for label in (ax.get_xticklabels()+ax.get_yticklabels() \
                          + ax.get_xticklabels(minor=True) \
                          +ax.get_yticklabels(minor=True)):
            bbox = label.get_window_extent()
            bboxi = bbox.inverse_transformed(figure.transFigure)
            bboxes.append(bboxi)
    
    # this is the bbox that bounds all the bboxes, again in relative
    # figure coords
    bbox = mtransforms.Bbox.union(bboxes)
    adjusted=adjust_figure_to_contain_bbox(figure,bbox)
    
    if adjusted:
        figure.canvas.draw()
    return False

def adjust_figure_to_contain_bbox(fig, bbox,pad=1.1):
    """
    For each amount we are over (in axes coordinates), we adjust by over*pad
    to give ourselves a bit of padding.
    """
    adjusted=False
    if bbox.xmin<0:
        fig.subplots_adjust(left=(fig.subplotpars.left-bbox.xmin*pad))
        adjusted=True
    if bbox.ymin<0:
        fig.subplots_adjust(bottom=(fig.subplotpars.bottom-bbox.ymin*pad))
        adjusted=True
    if bbox.xmax>1:
        fig.subplots_adjust(right=(fig.subplotpars.right-(bbox.xmax-1)*pad))
        adjusted=True
    if bbox.ymax>1:
        fig.subplots_adjust(top=(fig.subplotpars.top-(bbox.ymax-1)*pad))
        adjusted=True
    return adjusted

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


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to