[Tex/LaTex] How to incorporate and scale matplotlib plots in latex using the PGF backend

matplotlibtikz-pgftikzscale

I am using the pgf backend of matplotlib to generate pgf files which I want to put into my latex document using tikzscale to be able to resize them easily.

Example python code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib as mpl
mpl.use('pgf')
import numpy as np
import matplotlib.pyplot as mplp

mpl.rcParams['text.latex.unicode']=True
mpl.rcParams['text.usetex']=True
mpl.rcParams['pgf.texsystem'] = 'pdflatex'

fig = mplp.figure()
ax = fig.add_subplot(111)

x = np.arange(0,2 * np.pi, .1)
data = np.sin(x)

fmt = {"lw" : 3, "c" : "r", "ls" : '-'}

ax.plot(x, data, label=r"sample data with greek $\mu$", **fmt)

ax.set_ylabel(r"sample", rotation=0)
ax.legend()
fig.set_size_inches(1.41,1.)
# fig.savefig('./sample.pgf', dpi=500, bbox_inches='tight')
fig.savefig('./sample.pgf', dpi=500)

Example latex code:

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{pgfplots}

\pgfplotsset{compat=1.8}

\newcommand{\includepgf}[4]
{
  \begin{figure}
  \centering
  \includegraphics{#1}
  \includegraphics[width=#2\textwidth, height=#2*0.7071428571428572\textwidth]{#1}
  \includegraphics[width=#2\textwidth, axisratio=0.7071428571428572]{#1}
  \caption[]{#4}
  \label{#3}
  \end{figure}
}

\begin{document}
\includepgf{sample.pgf}{.9}{fig:sample}{Sample Figure}    
\end{document}

Unfortunately the following happens for the three different \includegraphics commands:

  1. Works as expected, includes plot in original size

  2. Compiles with

     Package tikzscale Warning: Scaling of sample.pgf's width was only
     (tikzscale)                accurate to 208.59694pt on input line 27.
    
    
     Package tikzscale Warning: Scaling of sample.pgf's height was only
     (tikzscale)                accurate to 147.29488pt on input line 27.
    

    no resizing in final pdf file.

  3. Does not compile, gives

     ! Package tikzscale Error: Requested to scale unscalable graphic.
    

Best Answer

enter image description here

I don't think includegraphics is the natural solution since you are creating a usable LaTeX file. As @Torbjørn T. has already mentioned, you should use the input command. Then, to solve your scaling problem, you might use

\scalebox{1.5}{\input{sample.pgf}}

for example; there is no preliminary LaTeX compilation involved.

  • In case you want to use includegraphics, it is natural to save your image as a png file in python.
  • Looking at the output of the python code, the pgf file, it seems to me that there are some cropping issues.

The code

\documentclass{article}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{pgfplots}

\usepackage{lipsum}
\begin{document}
\lipsum[1]

\begin{figure}[htb!]
  \centering
  \scalebox{.5}{\input{sample.pgf}}
  \scalebox{1}{\input{sample.pgf}}
  \scalebox{1.5}{\input{sample.pgf}}
  \caption{Sample figure modified with scalebox}
  \label{fig:1}
\end{figure}

\lipsum[2]
\end{document}
Related Question