我想绘制一个类似这样的函数
这是我目前的做法
\begin{tikzpicture}
\begin{axis}
\addplot[domain=0:1]{0.27};
\addplot[domain=1:2]{0.62};
\addplot[domain=2:3]{0.82};
\addplot[domain=3:4]{1};
\addplot[only marks,fill=white]coordinates{(0,0.27)(1,0.62)(2,0.82)(3,1)};
\addplot[only marks]coordinates{(0,0.05)(1,0.27)(2,0.62)(3,0.82)(4,1)};
\end{axis}
\end{tikzpicture}
不过,我想用每个线段的端点来绘制它们,比如
\begin{tikzpicture}
\begin{axis}
\addplot[mark=*]coordinates{(0,0)};
\addplot[domain=0:1,mark=*]coordinates{(0,0.27)(1,0.27)};
\addplot[domain=1:2,mark=*]coordinates{(1,0.62)(2,0.62)};
\addplot[domain=2:3,mark=*]coordinates{(2,0.82)(3,0.82)};
\addplot[domain=3:4,mark=*]coordinates{(3,1)(4,1)};
\end{axis}
\end{tikzpicture}
看起来像
如何单独设置标记以便底部图看起来像顶部图?
答案1
欢迎!通过最小限度的手术来打造discontinuous
发型这个答案能够选择性地打开垂直虚线
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\pgfplotsset{
discontinuous/.style={
scatter,
scatter/@pre marker code/.code={
\ifnodedefined{marker}{
\pgfpointdiff{\pgfpointanchor{marker}{center}}%
{\pgfpoint{0}{0}}%
\ifdim\pgf@y>0pt
\tikzset{options/.style={mark=*, fill=white}}
\path [vert] (marker-|0,0) -- (0,0);
\draw plot [mark=*] coordinates {(marker-|0,0)};
\else
\tikzset{options/.style={mark=none}}
\fi
}{
\tikzset{options/.style={mark=none}}
}
\coordinate (marker) at (0,0);
\begin{scope}[options]
},
scatter/@post marker code/.code={\end{scope}}
},/tikz/vert/.style={draw=none} %<- change to {draw,densely dashed} if needed
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[jump mark right,discontinuous] coordinates{(0,0) (0,0.27)(1,0.27)(1,0.62)(2,0.62)
(2,0.82)(3,0.82)(3,1)(4,1)};
\end{axis}
\draw[mark=*] plot coordinates {(marker)};
\end{tikzpicture}
\end{document}