[Tex/LaTex] pgfplots combining expression with marks at fixed x coordinates

pgfplotstikz-pgf

I would like pgfplots to draw an expression together with marks at pre-specified coordinates and/or fixed intervals. For instance, to plot the function 1/x as a line together with marks at discrete values 1, 2, …, 10.

This is the desired output:

enter image description here

This is my first failed attempt: based on the `samples' option. I want the points to be at fixed intervals, not random intervals (besides with a small value of samples the line becomes jagged, so not a good approach):

enter image description here

This is my second failed attempt: specifying the `mark indices' option.

enter image description here

I expected this last one to work. Am I doing it wrong? Thanks!

\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}

Best Answer

You can use samples at key:

\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}

enter image description here

To answer the comment regarding smoothness, one can use two \addplot commands, one with draw=none just to put the marks and the other with more samples to draw the curve as below:

\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}

enter image description here

Related Question