5

I'm trying to create plot with solid circles in the legend. Please see the image below for what I've got so far:

enter image description here

I'm unable to get my legend right, ideally I would like to draw solid red circle with label Maxima and solid blue circle with label Minima. However, I'm not sure how to create solid circles in legend.

Please see my code below:

\begin{tikzpicture}
  \begin{axis}[
    trig format plots=rad,
    axis lines = middle,
    enlargelimits,
    clip=false,
    legend entries={Minima,Maxima}
    ]
    \addplot[domain=-2*pi:2*pi,samples=200,black] {sin(x)};
    \fill[red] (axis cs: 0.5*pi,1.0) circle[radius=3pt];
    \fill[red] (axis cs: -1.5*pi,1.0) circle[radius=3pt];
    \fill[blue] (axis cs: -0.5*pi,-1.0) circle[radius=3pt];
    \fill[blue] (axis cs: 1.5*pi,-1.0) circle[radius=3pt];
    \draw[dotted,red!80] (axis cs: -1.5*pi,1.1) -- (axis cs: -1.5*pi,0);
    \draw[dotted,red!80] (axis cs: 0.5*pi,1.1) -- (axis cs: 0.5*pi,0);
    \draw[dotted,blue!80] (axis cs: 1.5*pi,-1.1) -- (axis cs: 1.5*pi,0.0);
    \draw[dotted,blue!80] (axis cs: -0.5*pi,-1.1) -- (axis cs: -0.5*pi,0.0);
    \addlegendimage{mark=ball,ball color=red,draw=red}
    \addlegendimage{mark=ball,ball color=blue,draw=blue}
  \end{axis}
\end{tikzpicture}

Any help or suggestions would be greatly appreciated.

1 Answer 1

5

For this simple case, you can fake it

\begin{tikzpicture}
\begin{axis}[
    trig format plots=rad,
    axis lines = middle,
    enlargelimits,
    clip=false,
    legend entries={Maxima,Minima}
    ]
    \addplot[domain=-2*pi:2*pi,samples=101, black,forget plot] {sin(x)};
    \addplot[domain=-3*pi/2:pi/2,samples=2,only marks,mark options=red] {sin(x)<0?NaN:sin(x)};
    \addplot[domain=-pi/2:3*pi/2,samples=2,only marks,mark options=blue] {sin(x)>0?NaN:sin(x)};
  \end{axis}
\end{tikzpicture}

enter image description here

2
  • 1
    Nice use of NaN --- bookmarked.
    – Rmano
    Jan 31, 2016 at 12:28
  • @ShivamKalra My pleasure
    – percusse
    Jan 31, 2016 at 21:08

You must log in to answer this question.

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