Call Python code with argument \addplot {\py{1+\x}};

The pythontex package allows us to do evaluate Python in LaTeX simply by \py{1+3}. However, this is not possible inside an addplot. A solution is wrapping the entire tikzpicture inside a substitute environment \pys, which substitutes !{1+3} with 4. However, I would like the result to depend on \x, which is not possible in this 'solution'.

How would it be possible to evaluate a piece of Python code with an argument from LaTeX, like this?

\documentclass{beamer}
\usepackage{pythontex}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


Thank you!

• Use a declare function={...} and inside it call python function. – percusse Nov 18 '17 at 9:40
• @percusse Thank you for your suggestion, but \pgfmathdeclarefunction{func}{1}{\pgfmathparse{\noexpand\py{1+#1}}} and \addplot {func(x)}; gives an undefined control sequence error for \pytx@type. – Carucel Nov 18 '17 at 13:20

The following works:

\documentclass{beamer}
\usepackage{pythontex}
\usepackage{pgfplots}
\pgfmathdeclarefunction{func}{1}{\pys{\pgfmathparse{!{str(1+float('#1'[2:-1])*(-1 if '#1'[0] == '2' else 1))}}}}
\begin{document}
\begin{tikzpicture}
\begin{axis}

Appearantly PGFplots or Tikz works with strange numbers of the form 2Y4.1665201e0, inwhich the first two letters are 1Y if it is positive and 2Y if it is negative.
Perhaps I need to do something with \pgfmathfloatvalueof?