From: Benjamin R. <ben...@ou...> - 2011-03-07 15:25:49
|
On Mon, Mar 7, 2011 at 4:36 AM, Yuri D'Elia <wa...@us...> wrote: > On Fri, 4 Mar 2011 14:57:34 -0600 > Benjamin Root <ben...@ou...> wrote: > > > Which version of matplotlib are you using? This example works for me > using > > the latest matplotlib from source. Also, why the awkward usage and > > Yes, with matplotlib 1.0 bbox_extra_artists now works. > > I consider bbox_extra_artists some kind of a hack (IMHO, all artists should > be considered with a 'tight' box), but coming from gnuplot/asymptote maybe > my point of view is biased. > What would be the point of a 'tight' box that excludes parts of the plot? I > would specify the coordinates myself if I needed clipping. > > > imports? If you want to force the Agg backend to be used, you could just > > do: > > > > import matplotlib > > matplotlib.use("Agg") > > > > before any other matplotlib imports. > > Thanks for the suggestion, that looks promising, but doesn't work: > > ---- > import matplotlib as mpl > mpl.use("Agg") > import matplotlib.figure > fig = mpl.figure.Figure() > fig.set_size_inches((20,20)) > plot = fig.add_subplot(111) > plot.set_title("Subtitle") > plot.plot([1,2,3], [3,2,1]) > st = fig.suptitle("Horray!", fontsize=20) > fig.savefig("out.png", bbox_inches='tight', bbox_extra_artists=[st]) > ---- > > 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. Also notice that I name the object coming from add_subplot() "ax" instead of "plot". This is for clarity and to avoid confusion with the command "plot". This is also the generally accepted coding convention in matplotlib. > > I find the documentation a bit scattered around this subject. > I'm not using the pyplot interface, so I guess that .use("Agg") doesn't do > anything for me? > I also have no reason to use the pyplot interface, why should I? I have no > matlab background, and I mostly use matplotlib procedurally (ie not > interactively). > The only accepted ways to use matplotlib are through either the pyplot or the pylab interfaces. This is why they are called interfaces. It does not matter if you are using matplotlib interactively or procedurally. I personally use the pyplot interface in all of my scripts, as I rarely ever do interactive plotting. The pylab interface is more geared towards interactive plotting. These interfaces are designed to make your life easier. Think of it this way. The developers can make many changes to matplotlib internals from one release to the next, but we do our best to make the interface as stable as possible. If you write code that do not utilize the pylab or pyplot interfaces, then your scripts will likely break at the next release. Trust me, a lot of your problems will be solved by picking an interface (I recommend pyplot) and sticking with it. I hope this is helpful, Ben Root |