3

I am trying to use decorations to add markers to some lines. The reason for using decorations, rather than simply markers, is that the data points I'm plotting are very dense: filtering them beforehand is not a viable option, and using markers results in an incomprehensible blob of overlapping things. Good results can be obtained doing something like the following MWE

\documentclass[11pt]{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
xmin=0, xmax=5,
ymin=0, ymax=2,
postaction={decoration={markings,
    mark=between positions 0 and 5 step 0.05
    with { \fill circle[radius=2pt]; }},
    decorate}
]
\addplot[samples=500]{1};
\addlegendentry{Plot 1};
\end{axis}
\end{tikzpicture}

\end{document}

However, the MWE above produces a legend where the decoration does not appear. How can I make the decoration appear also in the legend?

3
  • try adding other plots before and after your plot, you can mark selectively instead of this fragile method
    – percusse
    Nov 14, 2016 at 13:41
  • 3
    Try legend style={mark=*,mark size=2pt}. But maybe you can use markers instead decoration: \addplot[samples=500,mark=*,mark repeat=25]{1};
    – esdd
    Nov 14, 2016 at 14:17
  • @esdd, do you want to write your comment as an answer? If not, I would vote to close the question as being solved in the comments. Dec 11, 2016 at 9:38

1 Answer 1

1

You could add the marks to the every axis legend style using

legend style={mark=*,mark size=2pt}

as a key for the axis environment.

enter image description here

Code:

\documentclass[11pt]{article}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.14}% <- added, see the documentation 
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
xmin=0, xmax=5,
ymin=0, ymax=2,
postaction={decoration={markings,
    mark=between positions 0 and 5 step 0.05
    with { \fill circle[radius=2pt]; }},
    decorate},
    legend style={mark=*,mark size=2pt}% <- added
]
\addplot[samples=500]{1};
\addlegendentry{Plot 1};
\end{axis}
\end{tikzpicture}
\end{document}

But I would suggest to use markers instead decoration. You can reduce the number of marks using mark repeat=25. Then only each 25th point is marked.

enter image description here

Then the first mark on the plot is not clipped at the axis border.

\documentclass[11pt]{article}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.14}% <- added, see the documentation 
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
xmin=0, xmax=5,
ymin=0, ymax=2
]
\addplot[samples=500,mark=*,mark repeat=25]{1};% <- changed
\addlegendentry{Plot 1};
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .