The matplotlib PGF backend saves as raw PGF. What you probably want instead is pgfplots
, which you can get using tikzplotlib
, assuming your plots aren't too complex and all the features are supported.
Sample Python script for creating figure:
from pylab import *
from tikzplotlib import save as tikz_save
x = linspace(0, 10, 101)
plot(x, sin(x))
xlabel('$x$-axis')
ylabel('$y$-axis')
tikz_save('fig.tikz',
figureheight = '\\figureheight',
figurewidth = '\\figurewidth')
Sample accompanying LaTeX document that brings in the plots with two different dimensions (you could use \input
; I'm using \InputIfFileExists
so that missing figures won't kill compilation):
\documentclass{article}
\usepackage{pgfplots}
\newlength\figureheight
\newlength\figurewidth
\begin{document}
\setlength\figureheight{2in}
\setlength\figurewidth{3in}
\InputIfFileExists{fig.tikz}{}{\textbf{!! Missing graphics !!}}
\setlength\figureheight{3in}
\setlength\figurewidth{4in}
\InputIfFileExists{fig.tikz}{}{\textbf{!! Missing graphics !!}}
\end{document}
Output:
![enter image description here](https://i.stack.imgur.com/UFGVv.png)
Depending on what you are doing, you could also consider using my PythonTeX package to put everything in the LaTeX source:
\documentclass{article}
\usepackage{pgfplots}
\usepackage[stdout=false]{pythontex}
% matplotlib2tikz prints messages to stdout, so don't include
% stdout automatically; could also redirect stdout to avoid this
\setpythontexworkingdir{.}
% set PythonTeX to use the document root directory as the working
% directory, so that all plots will be saved there; could use
% another location, but then would need to specify a path when
% using \input and \InputIfFileExists
\newlength\figureheight
\newlength\figurewidth
\begin{document}
\begin{pycode}
from pylab import *
from tikzplotlib import save as tikz_save
x = linspace(0, 10, 101)
plot(x, sin(x))
xlabel('$x$-axis')
ylabel('$y$-axis')
tikz_save('fig.tikz',
figureheight = '\\figureheight',
figurewidth = '\\figurewidth')
\end{pycode}
\setlength\figureheight{2in}
\setlength\figurewidth{3in}
\InputIfFileExists{fig.tikz}{}{\textbf{!! Missing graphics !!}}
\setlength\figureheight{3in}
\setlength\figurewidth{4in}
\InputIfFileExists{fig.tikz}{}{\textbf{!! Missing graphics !!}}
\end{document}
The basic installer does only install some packages and the basic tex system. Therefore if you run TeX code using packages which are not installed the system asks to install them or does it if you have allowed that while the installation process.
In my opinion it is better to install the complete distribution. That needs more time, okay, but you can use your TeX distribution without the need to be always online ...
Your message
The directory is not a valid installation source source
simply means that you are not allowed to install there files. Could it be that you have not the right for that (user account instead of admin account)?
Please add the complete path you used to your question.
Best Answer
I encountered the same problem. The
PATH
variable that is set in my shell is correct and included my custombin
dirs, including Latex. However the one "seen" by Python doesn't. Here is what I'm using: