Confused about get_tightbbox() and renderer

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

Sorry, I forgot to use a sensible subject for the email!

Regards,

–Damon

···

On 12 Aug 2009, at 16:35, Damon McDougall wrote:

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

get_tightbbox is a bit experimental feature and it is discouraged for
an ordinary user (maybe the method should not be an public method).
Unless you understand how the internal transformation thing works, I'm
afraid there is not much thing you can do with its return value.

Instead, you should use the savefig function with bbox_inches="tight"
(it actually calls the get_tightbbox method with the proper renderer
for you). For example,

fig.savefig("a.pdf", bbox_inches="tight")

Another approach to eliminate the space is to adjust the subplot
parameters (note that the script you posted does not use Subplot, but
it can be easily modified).

http://matplotlib.sourceforge.net/faq/howto_faq.html?#automatically-make-room-for-tick-labels

-JJ

···

On Wed, Aug 12, 2009 at 11:35 AM, Damon McDougall<damon.mcdougall@...287...> wrote:

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

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Jae-Joon,

Brilliant! An even simpler solution! Thank you very much :slight_smile:

Figure.savefig() also takes pad_inches as a keyword argument and it's default is 0.1 inches. If you set it to 0.0 inches it can sometimes crop some of the x-axis label. I have found that if you do

fig.savefig("a.pdf", bbox_inches='tight', pad_inches=0.03)

It's pretty good. Just for anybody who was interested.

Thanks again, Jae-Joon!
Regards,
--Damon

···

On 12 Aug 2009, at 17:22, Jae-Joon Lee wrote:

get_tightbbox is a bit experimental feature and it is discouraged for
an ordinary user (maybe the method should not be an public method).
Unless you understand how the internal transformation thing works, I'm
afraid there is not much thing you can do with its return value.

Instead, you should use the savefig function with bbox_inches="tight"
(it actually calls the get_tightbbox method with the proper renderer
for you). For example,

fig.savefig("a.pdf", bbox_inches="tight")

Another approach to eliminate the space is to adjust the subplot
parameters (note that the script you posted does not use Subplot, but
it can be easily modified).

http://matplotlib.sourceforge.net/faq/howto_faq.html?#automatically-make-room-for-tick-labels

-JJ

On Wed, Aug 12, 2009 at 11:35 AM, Damon > McDougall<damon.mcdougall@...287...> wrote:

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

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus
on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users