2

I generate a step function in Python and export it as a tikzfigure, saved in the file step.tex.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib2tikz import save as tikz_save

plt.step(np.array([1, 2, 3, 4]), np.array([10, 5, 15, 20]))
tikz_save('step.tex')

The content of the step.tex file is

% This file was created by matplotlib2tikz v0.6.6.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
xmin=0.85, xmax=4.15,
ymin=4.25, ymax=20.75,
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0]
table {%
1 10
2 5
3 15
4 20
};
\end{axis}

\end{tikzpicture}

After importing this file in LaTex, using the code

\documentclass{report}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}
Hello\\
\input{step.tex}
\end{document}

I noticed that the step function is replaced by non-horizontal lines connecting the dots ('first-order hold' instead of 'zero-order hold', as if I had used plt.plot() instead of plt.step()):

enter image description here

Is this a problem on the Python side (that I should ask elsewhere), or on the LaTeX side?

  • 1
    Well, it's sort of both. The plot is wrong because the const plot key is missing, but const plot is missing because matplotlib2tikz has failed to properly "translate" the step plot to pgfplots code. Hence, I would report the problem at github.com/nschloe/matplotlib2tikz/issues – Torbjørn T. Aug 13 '17 at 16:12
  • Or rather, it's const plot mark right you want. – Torbjørn T. Aug 13 '17 at 16:15
2

The key is \addplot [const plot].


\documentclass{report}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
xmin=0.85, xmax=4.15,
ymin=4.25, ymax=20.75,
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black}
]
\addplot [semithick, color0,const plot]
table {%
1 10
2 5
3 15
4 20
};
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

See pgfplots documentation (Revision 1.15) in section 4.5.3 Constant Plots.

enter image description here

  • OK, I did not know this. You would expect that the matplotlib2tikz library would add this automatically. – Karlo Aug 13 '17 at 16:09
  • 1
    @Karlo Apparently, that's not the case :). – Dr. Manuel Kuehner Aug 13 '17 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.