\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{pgf}

\newcommand{\includepyplot}[1]{%
	\ifnum\pdfstrcmp{\pdffilemoddate{#1.py}}{\pdffilemoddate{pyplot-output/#1.pgf}}>0%
		\immediate\write18{%
			mkdir -p $(dirname pyplot-output/#1);%
			python3 #1.py > pyplot-output/#1.pgf%
		}%
	\fi%
	\noindent\input{pyplot-output/#1.pgf}%
}

\begin{document}

\section{Histogram}

\begin{figure}[h]
	\begin{center}
		\includepyplot{histogram}
    \end{center}
	\caption{A PGF histogram from \texttt{matplotlib}.}
\end{figure}

\end{document}
