0

How can I put a point (mark) in a PGFPlots graph at every x values?

For example, I have the following graph:

graph

Produced by the following code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable} % For \pgfplotstableread
\pgfplotsset{compat=1.10}

\begin{document}
\begin{tikzpicture}[baseline,trim axis left,trim axis right]
\begin{axis}[
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=2,
    /tikz/.cd
},
enlargelimits=true,
minor tick num=1,
width=\textwidth,
ylabel near ticks,
xlabel near ticks,
ytick={0,0.25,...,1},
xtick={0,1,...,4},
ylabel={Weight $w$},
xlabel={Slot $s$},
]
\addplot[domain=0:4,dashed]{0.31622776601683794^x};
\addplot[domain=0:4.0001,thick,const plot]{0.31622776601683794^floor(x)};
\end{axis}
\end{tikzpicture}
\end{document}

I tried using several mark primitives, mark step, mark phase, but was unable to produce the desired output. Desired output:

output

  • 1
    You could e.g. add samples=5 to the second plot. – Stefan Pinnow Feb 12 '17 at 2:51
2

That could be done either by just adding samples=5 to your code or you can even do it a bit simpler.

For more details have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat' level or higher to make use of the advanced positioning of labels
    \pgfplotsset{compat=1.3}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=\textwidth,
            ylabel={Weight $w$},
            xlabel={Slot $s$},
            % it is much simpler to use this key instead of defining the list
            xtick distance=1,
            minor tick num=1,
        ]
            \addplot [
                domain=0:4,
                dashed,
            ] {0.31622776601683794^x};
            \addplot [
                % define the marker you want to use ...
                mark=*,
                % ... and how it shall look like
                mark options={
                    color=orange,
                },
                % there is no need to use another domain here
                % (so you can move it to the axis options and delete it
                %  in the two `\addplot' options
                domain=0:4,
                % it seems you only want the values at the integers, so
                % define an appropriate number of samples
                samples=5,
                % and you want the marker on the left of the `const plot'
                const plot mark left,
                thick,
            % done all of the above there is no need for the floor function
            ] {0.31622776601683794^(x)};
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.