我该如何在 pgfplots 中创建一个简单的特征函数图?我的 Latex 处理器不需要基于函数生成图,手动绘制坐标和线条就足够了。pgfplots 是适合这项工作的工具吗?
答案1
这是一个简单的解决方案,使用pgfplots
包裹
% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{every axis/.append style={
axis x line=middle, % put the x axis in the middle
axis y line=middle, % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$}, % default put x on x-axis
ylabel={$y$}, % default put y on y-axis
},
cmhplot/.style={color=red,mark=none,line width=1pt,<->},
soldot/.style={color=red,only marks,mark=*},
holdot/.style={color=red,fill=white,only marks,mark=*},
}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3.5,xmax=5.5,
ymin=-1,ymax=2,
]
\addplot[cmhplot,<-,domain=-3:1]{0};
\addplot[cmhplot,-,domain=1:4]{1};
\addplot[cmhplot,->,domain=4:5]{0};
\addplot[holdot]coordinates{(1,0)(4,1)};
\addplot[soldot]coordinates{(1,1)(4,0)};
\end{axis}
\end{tikzpicture}
\end{document}
作为参考,这里有一个使用的解决方案pstricks
% arara: latex
% arara: dvips
% arara: ps2pdf
\documentclass[pstricks,border=2mm]{standalone}
\usepackage{pst-plot}
\psset{algebraic=true}
\begin{document}
\begin{pspicture}(-3.5,-1)(6,2)
\psaxes[dx=2,Dx=2]{<->}(0,0)(-3.5,-1)(6,2)[$x$,-90][$y$,0]
\psplot[linecolor=blue,arrows=<-o]{-3}{1}{0}
\psplot[linecolor=blue,arrows=*-o]{1}{4}{1}
\psplot[linecolor=blue,arrows=*->]{4}{5}{0}
\end{pspicture}
\end{document}