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:
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):
This is my second failed attempt: specifying the `mark indices' option.
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:To answer the comment regarding smoothness, one can use two
\addplot
commands, one withdraw=none
just to put the marks and the other with moresamples
to draw the curve as below: