我在 Python 中生成一个步进函数并将其导出为tikzfigure
,保存在文件中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')
该文件的内容step.tex
是
% 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}
在 LaTex 中导入此文件后,使用代码
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\begin{document}
Hello\\
\input{step.tex}
\end{document}
我注意到阶跃函数被连接点的非水平线所取代(“一阶保持”而不是“零阶保持”,就好像我使用了plt.plot()
而不是plt.step()
):
这是 Python 方面的问题(我应该去别处询问)还是 LaTeX 方面的问题?
答案1
关键是
\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}
看pgfplots
文档(修订版 1.15)部分4.5.3 常数图。