2

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

I insert the figure into Latex with the following:

\begin{figure}
    \centering
    \input{test_fig.pgf}
\end{figure}

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:

\documentclass{beamer}
\usepackage{beamerthemeshadow}
\usepackage{pgf}

\begin{document}
\title{Slide Title}  
\author{Author}
\date{\today}
\frame{\titlepage} 
\section{Section 1} 
\frame{\frametitle{Title}
\begin{figure}
    \centering
    \resizebox{0.8\textwidth}{!}{\input{test_fig.pgf}}
\end{figure}
}
\end{document}
Jonny
  • 783
  • 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 06 '16 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. – Torbjørn T. Sep 06 '16 at 19:51
  • @DaiBowen, indeed it does, but now the text size on the figure doesn't match the document font size anymore, which is kind of the reason why I want to use PGF. – Jonny Sep 06 '16 at 20:32
  • What about the answer in the question I link to? http://tex.stackexchange.com/a/218134/ – Dai Bowen Sep 06 '16 at 20:34
  • Seems a bit too complicated for me @DaiBowen. I will have to spend some time trying to figure that out. – Jonny Sep 06 '16 at 20:52
  • @TorbjørnT. I would like a more automatic way. I don't want to have to think about the figure size every time I make a plot in matplotlib. For example I would like to use the same code whether I'm using beamer or a normal doc. – Jonny Sep 06 '16 at 20:53
  • If you're able to post a compilable MWE with the matplotlib output you wish to work with this might make it easier for others to experiment and investigate in the mean time. – Dai Bowen Sep 06 '16 at 20:55
  • 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. – Torbjørn T. Sep 06 '16 at 21:39
  • @TorbjørnT. Is tikzscale usable here? (I've never used it - just seem other people use it in somewhat-possibly-similar cases.) – cfr Sep 07 '16 at 01:18
  • 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). – Torbjørn T. Sep 07 '16 at 05: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 07 '16 at 18:16
  • 1
    @Jonny do you still plan to produce such an answer? Otherwise I think this might as well be marked a duplicate of Scale .pgf image but preserve font size or Set the size of pgf picture? – Dai Bowen Oct 16 '16 at 12:05
  • @DaiBowen, I didn't get around to it and you are right, there is enough comonality. If you want you can mark it as a duplicate. – Jonny Oct 21 '16 at 17:23

0 Answers0