8

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.
    
  • 1
    Nils: If I were to take a wild guess, then I'd say that tikzscale perhaps doesn't work with pgfpictures. Workarounds could be to generate different .pgf files, with different values in fig.set_size_inches, or create a PDF by inputing sample.pgf in a separate .tex file and including that PDF with includegraphics. – Torbjørn T. Sep 20 '13 at 18:38
  • 1
    @TorbjørnT. You're probably right, the error messages seem pretty fundamental. Your workaround is viable, but it destroys the point in using the pgf backend, i.e. to have perfectly matched font sizes, colors, fonts, etc. I was under the impression that this is one of the main reasons the pgf backend was introduced. – Nils_M Sep 21 '13 at 10:19
  • Well, my first suggestion doesn't have that issue. The only problem there is that you have to know what size you want for the figure beforehand. You could take a look at using matplotlib2tikz, which spits out pgfplots code. tikzscale should work with pgfplots plots according to the manual. – Torbjørn T. Sep 21 '13 at 14:34
  • You're right, it would not destroy the point of using the backend. It would just introduce a restriction which can be annoying if I need to use the plot in a different layout. Ill look into matplotlib2tikz, thank you! – Nils_M Sep 23 '13 at 7:35
  • 1
    Some related questions: tex.stackexchange.com/questions/148296 tex.stackexchange.com/questions/53605 Alains suggestion from a comment, \makeatletter\pgfsys@transformxyscale{2}{2}\makeatother, will let you scale the pgf-image, but the text will be scaled as well, so it's not really a solution. – Torbjørn T. Mar 14 '14 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.