From: Benjamin R. <ben...@ou...> - 2011-03-07 16:59:35
|
On Mon, Mar 7, 2011 at 10:33 AM, Yuri D'Elia <wa...@th...> wrote: > On Mon, 7 Mar 2011 09:25:23 -0600 > Benjamin Root <ben...@ou...> wrote: > > > The problem is that you are creating your figure wrong. Try this: > > > > import matplotlib as mpl > > mpl.use("Agg") > > import matplotlib.pyplot as plt > > > > fig = plt.figure(figsize=(20, 20)) > > ax = fig.add_subplot(111) > > ax.set_title("Subtitle") > > ax.plot([1, 2, 3], [3, 2, 1]) > > st = fig.suptitle("Horray!", fontsize=20) > > fig.savefig("out.png", bbox_inches='tight', bbox_extra_artists=[st]) > > > > Notice that I am using the pyplot interface. Matplotlib was intended to > be > > used through either this pyplot interface or the pylab interface (which I > do > > not personally recommend if you want full control over your plots). If > you > > don't use either interfaces, then don't be surprised when things do not > work > > properly. In particular, creating the figure object by directly calling > the > > Figure constructor bypasses important steps that involves attaching the > > appropriate backend to the figure object. > > I was reading this at the time: > > http://matplotlib.sourceforge.net/faq/usage_faq.html > > I inferred pyplot was just a matlab-like interface on top of matplotlib, > and figured using directly the matplotlib was acceptable. > Yeah, I am guessing that page is a little out-dated and could be better worded. However, the page does say that the preferred style is the pyplot interface. Also notice that it is extremely rare for any of the documentation to directly create the matplotlib objects without the pyplot or pylab interface. The pointof the statement "MATLAB-like" is that most of the plotting functions available in MATLAB are also available in matplotlib. In addition, the phrase "more MATLAB-like" is meant to state that various mathematical functions are made available as well. This was designed to make transitions from MATLAB to matplotlib easier for the user. Ultimately, we desire that the user transitions completely over to the pyplot interface. Note that this has nothing to do with interactivity or proceedural. If anything, pylab was more intended for interactivity because its syntax is more concise, but you lose a lot of control. > Reading the documentation of the single objects of matplotlib was enough to > get me started. I see pyplot as having more shortcuts for common operations, > but I was unsure how far can I could go by using pyplot only. > > Think of it this way. Matplotlib depends a lot upon hierarchical design. The pyplot (or pylab) interfaces sit on top of that heirarchy and represents the matplotlib environment. This environment creates figures. These figures can many components, the most important of which are one or more axes objects. Each axes object has two or more axis objects and are responsible for plotting themselves. While there are some convenience functions through pyplot for doing some things like setting an axis label, it is not required to use pyplot for that. You can (and often should) use the axes object's method for doing so. The point is that you can use the matplotlib objects for a lot of things, and it is great that you are embracing the object oriented nature of matplotlib. However, your problem was caused by-passing the hierarchy: The interface should create the figure objects, the figure objects should create the axes objects, the axes objects should create the axis objects, and so on and so forth. I hope that makes it clearer for you, and I hope you continue to use matplotlib and find it useful! Ben Root |