From: Daniel M. <dan...@go...> - 2011-02-26 14:41:36
|
I have slightly modified the example from http://matplotlib.sourceforge.net/faq/howto_faq.html#automatically-make-room-for-tick-labels in order to demonstrate what I mean. It works with the manual string tick labels but not with regular auto-generated numerical ones. Maybe someone knows how to fix this? And I *really* think this should work automatically. As a compromise, maybe an rcParam would help in order to keep the current dumb behavior... Thanks in advance, Daniel import matplotlib.pyplot as plt import matplotlib.transforms as mtransforms fig = plt.figure(figsize=(5,3)) ax = fig.add_subplot(111) #ax.plot(range(10)) #ax.set_yticks((2,5,7)) #labels = ax.set_yticklabels(('really, really, really', 'long', 'labels')) ax.plot(range(100),[100000]*100) labels = ax.get_yticklabels() def on_draw(event): bboxes = [] for label in labels: bbox = label.get_window_extent() print bbox # the figure transform goes from relative coords->pixels and we # want the inverse of that bboxi = bbox.inverse_transformed(fig.transFigure) bboxes.append(bboxi) # 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 fig.canvas.draw() return False fig.canvas.mpl_connect('draw_event', on_draw) plt.show() |