Hello all,

So I’m trying to use matplotlib’s OO interface (so programming without using ‘from pylab import *’) and found this useful page:http://matplotlib.sourceforge.net/leftwich_tut.txt after much googling.

My problem is that, in general, after producing a plot, I would open the .pdf produced to find lots of whitespace that I don’t want. The reason I don’t want the whitespace is that I want to include these figures in a latex document and I want to maximise space. I did some reading and from what I understand, Axes.get_tightbbox() is the correct tool to use to return a tight bounding box which I can then use to adjust the Axes limits. Here is the code I currently have:

import numpy as np

import matplotlib

fig_width_pt = 483.69687 # figure width in pt as returned by \showthe in LaTeX

inches_per_pt = 1.0/72.27

golden_ratio = (np.sqrt(5) - 1.0) / 2.0

fig_width_in = fig_width_pt * inches_per_pt # figure width in inches

fig_height_in = fig_width_in * golden_ratio # figure height in inches

fig_dims = [fig_width_in, fig_height_in] # fig dims as a list

matplotlib.use(‘PDF’)

matplotlib.rc(‘font’,**{‘family’:‘serif’,‘serif’:[‘Computer Modern Roman’]})

matplotlib.rc(‘text’, usetex=True)

matplotlib.rc(‘axes’, labelsize=10)

matplotlib.rc(‘legend’, fontsize=10)

matplotlib.rc(‘xtick’, labelsize=10)

matplotlib.rc(‘ytick’, labelsize=10)

matplotlib.rc(‘font’, size=10)

matplotlib.rc(‘figure’, figsize=fig_dims)

from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas

from matplotlib.figure import Figure

fig = Figure()

canvas = FigureCanvas(fig)

ax.fig_add_axes([0.2, 0.2, 0.5, 0.7])

ax.hold(True)

ax.grid(True)

plot_means = ax.plot(means, ‘b’, label=’$m_k$’)

plot_vars = ax.plot(vars, ‘g’, label=’$\sigma_k^2$’)

plot_ictruth = ax.axhline(y = x_0, xmin = 0, xmax = numtimes, color=‘r’, label=’$x_0$’)

ax.set_xlabel(’$k$’)

ax.legend(loc=‘upper right’)

tightbox = ax.get_tightbbox()

canvas.print_pdf(a)

The problem here is that get_tightbbox() takes 2 arguments, namely self and renderer. My question is, what is a renderer and how do I instantiate/create one? After some reading I think it’s something to do with maplotlib.backend_bases or something. Am I on the right track? After the call I want to adjust the Axes limits to the thing returned by get_tightbbox(), would ax.set_position(tightbox) do that here?

Any help would be greatly appreciated.

Regards,

–Damon