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:
-
Works as expected, includes plot in original size
-
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.
-
Does not compile, gives
! Package tikzscale Error: Requested to scale unscalable graphic.
Best Answer
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 theinput
command. Then, to solve your scaling problem, you might usefor example; there is no preliminary LaTeX compilation involved.
includegraphics
, it is natural to save your image as apng
file inpython
.python
code, thepgf
file, it seems to me that there are some cropping issues.The code