I created a plot in matplotlib and saved it as a PGF file.

I insert the figure into Latex with the following:


With \usepackage{pgf} in the preamble.

The figure is then inserted but its to big, i.e. stretching far off the page.

I should mention that this is in a beamer document class.

Is there a method to scale the figure so that its as wide as the \textwidth?

I have tried inserting the figure with \includegraphics[width=\textwidth]{test_fig.pgf}, but I get the error Unknown graphics extension: .pgf.

Here is a MWE, with the PGF figure attached here:


\title{Slide Title}  
\section{Section 1} 
  • 1
    Does \resizebox{0.8\textwidth}{!}{\input{test_fig.pgf}} give you the right control? Scale .pgf image but preserve font size may be of use.
    – Dai Bowen
    Sep 6, 2016 at 19:22
  • 5
    Set the size in matplotlib before/when saving. E.g. fig,ax=plt.subplots(figsize=(4,3)), which will make the figure 4 inches wide and 3 inches tall. Sep 6, 2016 at 19:51
  • 3
    I can see that, though I personally do just that, in order to avoid having to scale figures. What you could do is set edit your matplotlibrc file and set figure.figsize to around 4 in x 3 in (it's 8x6 by default). The \textwidth in your beamer example is about 10.8cm, and in a default article it is 12.2cm, so a 10cm wide figure would fit reasonably well in both cases. You could also have a look at matplotlib2tikz, which generates pgfplots code that is higher level and easier to modify. Sep 6, 2016 at 21:39
  • 1
    @cfr I don't think so, though I haven't tried. I seem to remember another question where someone tried and failed to use tikzscale, I don't think it's made for the low-level pgf code the pgf backend creates. On the other hand, it would work with the output from matplotlib2tikz (from the same author as matlab2tikz). Sep 7, 2016 at 5:46
  • 1
    I used matplotlib2tikz and I think its the best workflow. And you can set it so that you can set the figure size in Latex, making it super easy to scale the figure. I will post an answer to my question with a step-by-step guide on how to do this.
    – Jonny
    Sep 7, 2016 at 18:16


Browse other questions tagged or ask your own question.