我正在尝试创建带有图例中实心圆圈的图。请参见下图了解我目前得到的结果:
我无法正确绘制图例,理想情况下,我想绘制带有标签 Maxima 的实心红色圆圈和带有标签 Minima 的实心蓝色圆圈。但是,我不确定如何在图例中创建实心圆圈。
请参阅下面的代码:
\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}
任何帮助或建议都将不胜感激。
答案1
对于这个简单的案例,你可以假装
\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}