pgfplots 将表达式与固定 x 坐标处的标记相结合

pgfplots 将表达式与固定 x 坐标处的标记相结合

我希望 pgfplots 绘制一个表达式,并在预先指定的坐标和/或固定间隔处做标记。例如,将函数 1/x 绘制为一条线,并在离散值 1、2、...、10 处做标记。

这是所需的输出:

在此处输入图片描述

这是我第一次失败的尝试:基于“样本”选项。我希望点的间隔是固定的,而不是随机的(此外,样本值较小时,线会变得锯齿状,所以不是一个好方法):

在此处输入图片描述

这是我第二次失败的尝试:指定“标记索引”选项。

在此处输入图片描述

我以为最后一个会起作用。我做错了吗?谢谢!

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}

% Workaround: specify the data points
\begin{tikzpicture}
\begin{axis}[title = {use `coordinates'}, xtick = {0,1,...,10}, grid = both]
\addplot [mark=*] coordinates {(1,1) (2,0.5) (3,0.33) (4,0.25) (5,0.2) (6,0.167) (7,0.143) (8,0.125) (9,0.111) (10,0.1)};
\end{axis}
\end{tikzpicture}

% Desired approach: specify the expression and show marks at 1-unit intervals

% attempt 1: 
\begin{tikzpicture}
\begin{axis}[title = {use `samples'}, xtick = {0,1,...,10}, grid = both]
\addplot [domain = 0:10, mark=*, samples = 10] expression {1/x};
\end{axis}
\end{tikzpicture}

% attempt 2: 
\begin{tikzpicture}
\begin{axis}[title = {use `mark indices'}, xtick = {0,1,...,10}, grid = both]
\addplot [domain = 0:10, mark=*, mark indices = {0,1,...,10}] expression {1/x};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

您可以使用samples at密钥:

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[title = {use \texttt{samples at =<coordinate list>}}, xtick = {0,1,...,10}, grid = both]
\addplot [domain = 0:10, mark=*, samples at = {1,...,10}] expression {1/x};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

为了回答有关平滑度的评论,可以使用两个\addplot命令,一个命令draw=none仅用于放置标记,另一个命令用于samples绘制更多曲线,如下所示:

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[title = {use \texttt{samples at =<coordinate list>}}, xtick = {0,1,...,10}, grid = both]
\addplot [domain = 1:10, mark=none, samples=100] expression {1/x};
\addplot [draw=none,domain = 0:10, mark=*, samples at = {1,...,10}] expression {1/x};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

您有 10 个等长间隔。第一个样本位于图的开头,最后一个样本位于图的结尾。因此您至少需要samples=11(1+10*1)。如果两个标记之间应该有 20 个样本,则需要samples=201(1+10*20)。然后您可以使用类似

mark phase=0,mark repeat=20,samples=201

或者

mark indices={1,21,...,201},samples=201

请注意mark phase以 开头,0mark indices以 开头1

\documentclass[border=3pt,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}

% Desired approach: specify the expression and show marks at 1-unit intervals

% attempt 1: 
\begin{tikzpicture}
\begin{axis}[title = {use `samples and smooth'}, xtick = {0,1,...,10}, grid = both]
\addplot [domain = 0:10, mark=*, samples = 11,smooth] expression {1/x};
\end{axis}
\end{tikzpicture}

% attempt 2: 
\begin{tikzpicture}
\begin{axis}[title = {use `mark phase and mark repeat'}, xtick = {0,1,...,10}, grid = both]
\addplot [restrict y to domain=0:1,domain = 0:10, mark=*,mark phase=0,mark repeat=20,samples=201] expression {1/x};
\end{axis}
\end{tikzpicture}

% attempt 3: 
\begin{tikzpicture}
\begin{axis}[title = {use `mark indices'}, xtick = {0,1,...,10}, grid = both]
\addplot [restrict y to domain=0:1,domain = 0:10, mark=*,mark indices={1,21,...,201},samples=201] expression {1/x};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容