Scale .pgf image but preserve font size

Using Matplotlib, I generated a .pgf image.

import matplotlib
matplotlib.rcParams.update({"pgf.texsystem": "pdflatex"})
from matplotlib.pyplot import subplots, show
from numpy import linspace
x = linspace(0, 100, 30)
fig, ax = subplots(figsize = (10, 6))
ax.scatter(x, x)
fig.tight_layout()
fig.savefig('figure.pgf')


Which generates a long file: http://pastebin.com/tjLtTi5Q . I include this image in the document by using

    \usepackage{pgf}
\input{testfig.pgf}


I would like to scale it in such a way that the font size remains the same. Preferably by wrapping something around the \input{...} rather than changing the pgf file, though the later is acceptable if that's the only way.

Previous attempts include:

• \begin{pgfpicture}[scale=0.5] found here, but scale only exists for tikzpicture
• Trying to apply pgfmagnify, which I found here but can't get to work and can't find much documentation

\begin{pgfmagnify}{2}{2}
\begin{pgfpicture}
....
\end{pgfmagnify}

• Using \begin{pgfpicture}[width=0.5] which also doesn't do anything.

• Using resizebox as mentioned here:

\resizebox{0.7\textwidth}{!}{\input{figure.pgf}}


which (perhaps obviously) does not preserve font size.

EDIT: picture using the suggestion in the comment by Harish Kumar:

Part of the file that seems related to fonts:

\pgftext[x=0.501680in,y=2.691389in,right,]{{\sffamily\fontsize{12.000000}{14.400000}\selectfont 40}}%

• Although the picture still renders and does scale, the font size is not preserved... Is there some way to fix that?
– Mark
Dec 5, 2013 at 0:30
• @Mark: Wouldn't the easiest thing be to adjust the figsize = (10, 6) to figsize = (5, 3) (or something like that) in your Python code? That way, the font size remains unchanged, but the overall size of the figure is reduced.
– Jake
Dec 6, 2013 at 10:15
• @Jake: That seems to be the only fallback, but has the downside that I need to find the linewidth and regenerate all the images if I want to use them in different documents or change a margin or something. I kind of thought that the advantage of pgf would be that I wouldn't need to do that...
– Mark
Dec 6, 2013 at 10:20
• @Mark: Ah yes, I see. Yeah, that is the advantage of using PGF/TikZ, but I think the Matplotlib PGF backend doesn't use the best approach for that. The Matlab matlab2tikz library for instance generates PGFPlots code, which is much easier to adapt using styles within your LaTeX document, instead of generating low-level PGF code. In your situation, I would recommend exporting the data from Python and doing the plotting using entirely using "real" PGFPlots code, instead of exporting from Matplotlib.
– Jake
Dec 6, 2013 at 10:26
• That would work, but if there is any solution that does not involve completely rewriting all my plotting code and moving to another language then that would be preferred...
– Mark
Dec 9, 2013 at 2:07

You can scale the graphics by k using for example scalebox and scale the font by 1/k to compensate this. If you compile with XeLaTeX or LuaLaTeX you can use the scale features of fontspec.

Here is a simple example :

% to compile with XeLaTeX or LuaLaTeX
\documentclass[varwidth,border=50]{standalone}
\usepackage{fontspec}
\usepackage{pgf}

\begin{document}

% font scale = .25*4 = 1
\setsansfont{Latin Modern Sans}[Scale=.25]%
\scalebox{4}{
\begin{pgfpicture}
\pgfsetstrokecolor{red}%
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{5mm}{5mm}}%
\pgfusepath{draw}%
\pgftext[x=4,y=4]{{\sffamily\fontsize{12.000000}{14.400000}\selectfont 40}}%
\end{pgfpicture}
}

% font scale = .5*2 = 1
\setsansfont{Latin Modern Sans}[Scale=.5]%
\scalebox{2}{
\begin{pgfpicture}
\pgfsetstrokecolor{red}%
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{5mm}{5mm}}%
\pgfusepath{draw}%
\pgftext[x=4,y=4]{{\sffamily\fontsize{12.000000}{14.400000}\selectfont 40}}%
\end{pgfpicture}
}

\end{document}


• And what can be done is pdflatex is required? Then the fontspec package throws issues. Aug 4, 2017 at 18:02
• One idea is to redefine the command that sets the font size inside your pgfpicture. You can ask another question with minimal example of your case and reference to this question if you need more help.
– Kpym
Aug 5, 2017 at 6:37

You have full control over the plot dimensions in the axis environment of tikzpicture.

\begin{tikzpicture}
\begin{axis}[
width=\textwidth,
height=\textwidth
]
...
\end{tikzpicture}


Here you can replace \textwidth with any valid latex dimension, and the font sizes match the document font no matter what you set width and height to.

This will require editing the pgf file, which you say you don't want to do, but I honestly think this is the easiest way.

I highly suggest you look into matplotlib2tikz, which generated a pgf file that is much easier to work with than the native matplotlib pgf backend.