[Tex/LaTex] Scale .pgf image but preserve font size

fontsizescalingtikz-pgf

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:

enter image description here

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}}%

Best Answer

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}

enter image description here