John Hunter wrote: > On Thu, Jul 3, 2008 at 8:42 AM, John Kitchin <[EMAIL PROTECTED]> wrote: >> Thanks Matthias. That is a helpful example. >> >> I have been trying to figure out how to recursively examine all the objects >> in fig to see if there is a particular settable property. It seems like the >> algorithm has to be recursive so that it goes deep into all the lists, etc. >> I have not figured out how to know when you have reached the bottom/end of a >> trail. >> >> Such a function would let me set any text property in the whole figure >> without needing to know if it was a text object, label, legend, etc... maybe >> that is not as valuable as I think it would be though. > > This is a good idea, and I just added an artist method "findobj" in > svn that recursively calls get_children and implements a match > criteria (class instance or callable filter). There is also a > pyplot/pylab wrapper that operates on current figure by default. Here > is an example: > > > > import numpy as np > import matplotlib.pyplot as plt > import matplotlib.text as text > > a = np.arange(0,3,.02) > b = np.arange(0,3,.02) > c = np.exp(a) > d = c[::-1] > > fig = plt.figure() > ax = fig.add_subplot(111) > plt.plot(a,c,'k--',a,d,'k:',a,c+d,'k') > plt.legend(('Model length', 'Data length', 'Total message length'), > 'upper center', shadow=True) > plt.ylim([-1,20]) > plt.grid(False) > plt.xlabel('Model complexity --->') > plt.ylabel('Message length --->') > plt.title('Minimum Message Length') > > # match on arbitrary function > def myfunc(x): > return hasattr(x, 'set_color') > > for o in fig.findobj(myfunc): > o.set_color('blue') > > # match on class instances > for o in fig.findobj(text.Text): > o.set_fontstyle('italic')
Great! I used to write many such functions for setting font sizes of all elements in a figure. But speaking about the font sizes, one usually wants the title to be in larger font then the axis labels etc. How could something like this be implemented within your general findobj()? Just for the reference, this is how I did it: def setAxesFontSize( ax, size, titleMul = 1.2, labelMul = 1.0 ): """size : tick label size, titleMul: title label size multiplicator, labelMul: x, y axis label size multiplicator""" labels = ax.get_xticklabels() + ax.get_yticklabels() for label in labels: label.set_size( size ) labels = [ax.get_xaxis().get_label(), ax.get_yaxis().get_label()] for label in labels: label.set_size( labelMul * size ) for child in ax.get_children(): if isinstance( child, pylab.Text ): child.set_size( titleMul * size ) Maybe it could be implemented in the sense of: def myfontsizes( x ): """Depending on class of x, return also suggested value of the font size.""" for o, size in fig.findobj( myfontsizes, suggest_value = True ): o.set_size( size ) # Default for suggest_value is False... for o in fig.findobj(text.Text): o.set_fontstyle('italic') What do you think? r. ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08 _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users