10

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.
    
projetmbc
  • 13,315
Nils_M
  • 201
  • 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
  • 2
    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 07:35
  • Ok, I tried matplotlib2tikz, it has its issues. Legends to not work as expected, errorbar plots do not work, etc. It seems, that there really is no perfect way of creating plots in matplotlib and use them in latex. I very much anticipate the need of using my plots in slides, on posters and in journal documents, which will all have very different layouts. Seems i'll be forced to redo my pgf files each time. – Nils_M Sep 23 '13 at 09:47
  • Sorry for resurrecting, but couldn't Torbjørn's suggestion of putting everything inside a .tex work if the pgf is embedded into a tex file, then included directly into the main latex document WITHOUT manually creating a pdf? – Pepe Mandioca Nov 28 '13 at 03:43
  • Do I create an intermediary pdf file in the above code? I was under the impression, that using includegraphics on a pgf wouldnt do that, but would just use pgfplots to create the graphic. – Nils_M Dec 02 '13 at 10:06
  • 1
    Some related questions: http://tex.stackexchange.com/questions/148296/ http://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

1 Answers1

0

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}

Daniel N
  • 5,687