更改图例图像中线条的颜色(tikz pgfplot)

更改图例图像中线条的颜色(tikz pgfplot)

我想改变图例中线条的颜色,因为我的图中的线条颜色与标记颜色不同。

\begin{figure}[h!]
\begin{tikzpicture}
\begin{axis}[legend pos=south east, scale = 2, legend entries={only marks,{only marks, sharp plot}}]
\addplot [dgreen, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(1,0.188784)(2,0.272535)(3,0.370827)(14,0.719469)(15,0.712242)(16,0.718166)(17,0.697843)(19,0.706615)(23,0.703975)(28,0.692325)(30,0.685839)};
\addlegendentry{A}
\addplot [aquamarine, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(9,0.718341)(26,0.707893)};
\addlegendentry{B}
\addplot [navy, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(18,0.700465)};
\addlegendentry{C}
\addplot [black, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(11,0.762515)(12,0.752225)(13,0.736601)};
\addlegendentry{F}
\addplot [brown, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(10,0.724722)(20,0.711727)(21,0.701815)(22,0.699268)(24,0.708654)(25,0.710309)(27,0.698165)(29,0.693149)};
\addlegendentry{D}
\addplot [beige, mark size= 1pt, only marks, mark = *,mark options=solid, legend image post style={sharp plot}] coordinates
{(4,0.489534)(5,0.545647)(6,0.566121)(7,0.634745)(8,0.662385)};
\addlegendentry{E}

\addplot + [silver, dotted, mark size= 1pt, mark = *,mark options=solid] coordinates
{(1,0.144262)(2,0.159016)(3,0.27174)(4,0.43602)(5,0.458619)(6,0.483743)(7,0.51041)(8,0.573646)(9,0.62229)(10,0.65154)(11,0.70702)(12,0.673687)(13,0.675257)(14,0.667425)(15,0.667022)(16,0.644954)(17,0.644954)(18,0.584561)(19,0.65154)(20,0.62957)(21,0.62957)(22,0.62957)(23,0.62957)(24,0.658051)(25,0.658051)(26,0.632539)(27,0.643377)(28,0.575812)(29,0.575812)(30,0.5809)};
\addlegendentry{min}

\addplot + [silver, dotted, mark size= 1pt, mark = *,mark options=solid] coordinates
{(1,0.249641)(2,0.367719)(3,0.522434)(4,0.601786)(5,0.648929)(6,0.654226)(7,0.681595)(8,0.806531)(9,0.836739)(10,0.809737)(11,0.801275)(12,0.829309)(13,0.836739)(14,0.792238)(15,0.801766)(16,0.801766)(17,0.753377)(18,0.823641)(19,0.772153)(20,0.778706)(21,0.773941)(22,0.773941)(23,0.769177)(24,0.798295)(25,0.798295)(26,0.785076)(27,0.773941)(28,0.769177)(29,0.769177)(30,0.769177)};
\addlegendentry{max}

\draw[silver] (axis cs:1,0.188784) -- (axis cs:2,0.272535);
\draw[silver] (axis cs:2,0.272535) -- (axis cs:3,0.370827);
\draw[silver] (axis cs:3,0.370827) -- (axis cs:4,0.489534);
\draw[silver] (axis cs:4,0.489534) -- (axis cs:5,0.545647);
\draw[silver] (axis cs:5,0.545647) -- (axis cs:6,0.566121);
\draw[silver] (axis cs:6,0.566121) -- (axis cs:7,0.634745);
\draw[silver] (axis cs:7,0.634745) -- (axis cs:8,0.662385);
\draw[silver] (axis cs:8,0.662385) -- (axis cs:9,0.718341);
\draw[silver] (axis cs:9,0.718341) -- (axis cs:10,0.724722);
\draw[silver] (axis cs:10,0.724722) -- (axis cs:11,0.762515);
\draw[silver] (axis cs:11,0.762515) -- (axis cs:12,0.752225);
\draw[silver] (axis cs:12,0.752225) -- (axis cs:13,0.736601);
\draw[silver] (axis cs:13,0.736601) -- (axis cs:14,0.719469);
\draw[silver] (axis cs:14,0.719469) -- (axis cs:15,0.712242);
\draw[silver] (axis cs:15,0.712242) -- (axis cs:16,0.718166);
\draw[silver] (axis cs:16,0.718166) -- (axis cs:17,0.697843);
\draw[silver] (axis cs:17,0.697843) -- (axis cs:18,0.700465);
\draw[silver] (axis cs:18,0.700465) -- (axis cs:19,0.706615);
\draw[silver] (axis cs:19,0.706615) -- (axis cs:20,0.711727);
\draw[silver] (axis cs:20,0.711727) -- (axis cs:21,0.701815);
\draw[silver] (axis cs:21,0.701815) -- (axis cs:22,0.699268);
\draw[silver] (axis cs:22,0.699268) -- (axis cs:23,0.703975);
\draw[silver] (axis cs:23,0.703975) -- (axis cs:24,0.708654);
\draw[silver] (axis cs:24,0.708654) -- (axis cs:25,0.710309);
\draw[silver] (axis cs:25,0.710309) -- (axis cs:26,0.707893);
\draw[silver] (axis cs:26,0.707893) -- (axis cs:27,0.698165);
\draw[silver] (axis cs:27,0.698165) -- (axis cs:28,0.692325);
\draw[silver] (axis cs:28,0.692325) -- (axis cs:29,0.693149);
\draw[silver] (axis cs:29,0.693149) -- (axis cs:30,0.685839);

\end{axis}
\end{tikzpicture}
\caption{Maximum Plot}

正如您在图片中看到的,我在图例中为图 E 添加了线条,但我无法更改图例中图线的颜色。 阴谋

答案1

我不太明白你为什么要这么做,但这里有另一种方法。在绘图选项中E,添加forget plot,这样它就不会包含在图例中,然后添加

\addlegendimage{mark=*,mark size=1pt,mark options={color=Beige},color=Silver}
\addlegendentry{E}

color正如您所见,您可以通过在 中分别设置标记的颜色,而不影响线条的颜色mark options

此外,绘制线条时Silver不需要长序列的\draw命令,只需使用\addplot

在此处输入图片描述

\documentclass[svgnames,border=4mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=south east]
\addplot [green, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(1,0.188784)(2,0.272535)(3,0.370827)(14,0.719469)(15,0.712242)(16,0.718166)(17,0.697843)(19,0.706615)(23,0.703975)(28,0.692325)(30,0.685839)};
\addlegendentry{A}
\addplot [Aquamarine, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(9,0.718341)(26,0.707893)};
\addlegendentry{B}
\addplot [Navy, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(18,0.700465)};
\addlegendentry{C}
\addplot [black, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(11,0.762515)(12,0.752225)(13,0.736601)};
\addlegendentry{F}
\addplot [brown, mark size= 1pt, only marks, mark = *,mark options=solid] coordinates
{(10,0.724722)(20,0.711727)(21,0.701815)(22,0.699268)(24,0.708654)(25,0.710309)(27,0.698165)(29,0.693149)};
\addlegendentry{D}
\addplot [Beige, mark size= 1pt, only marks, mark = *,mark options=solid,forget plot] coordinates
{(4,0.489534)(5,0.545647)(6,0.566121)(7,0.634745)(8,0.662385)};

\addlegendimage{mark=*,mark size=1pt,mark options={color=Beige},color=Silver}
\addlegendentry{E}

\addplot + [Silver, dotted, mark size= 1pt, mark = *,mark options=solid] coordinates
{(1,0.144262)(2,0.159016)(3,0.27174)(4,0.43602)(5,0.458619)(6,0.483743)(7,0.51041)(8,0.573646)(9,0.62229)(10,0.65154)(11,0.70702)(12,0.673687)(13,0.675257)(14,0.667425)(15,0.667022)(16,0.644954)(17,0.644954)(18,0.584561)(19,0.65154)(20,0.62957)(21,0.62957)(22,0.62957)(23,0.62957)(24,0.658051)(25,0.658051)(26,0.632539)(27,0.643377)(28,0.575812)(29,0.575812)(30,0.5809)};
\addlegendentry{min}

\addplot + [Silver, dotted, mark size= 1pt, mark = *,mark options=solid] coordinates
{(1,0.249641)(2,0.367719)(3,0.522434)(4,0.601786)(5,0.648929)(6,0.654226)(7,0.681595)(8,0.806531)(9,0.836739)(10,0.809737)(11,0.801275)(12,0.829309)(13,0.836739)(14,0.792238)(15,0.801766)(16,0.801766)(17,0.753377)(18,0.823641)(19,0.772153)(20,0.778706)(21,0.773941)(22,0.773941)(23,0.769177)(24,0.798295)(25,0.798295)(26,0.785076)(27,0.773941)(28,0.769177)(29,0.769177)(30,0.769177)};
\addlegendentry{max}

\addplot [mark=none,Silver] coordinates  {
(1,0.188784)
(2,0.272535)
(3,0.370827)
(4,0.489534)
(5,0.545647)
(6,0.566121)
(7,0.634745)
(8,0.662385)
(9,0.718341)
(10,0.724722)
(11,0.762515)
(12,0.752225)
(13,0.736601)
(14,0.719469)
(15,0.712242)
(16,0.718166)
(17,0.697843)
(18,0.700465)
(19,0.706615)
(20,0.711727)
(21,0.701815)
(22,0.699268)
(23,0.703975)
(24,0.708654)
(25,0.710309)
(26,0.707893)
(27,0.698165)
(28,0.692325)
(29,0.693149)
};
\end{axis}
\end{tikzpicture}
\end{document}

答案2

我刚刚解决了...如果你添加

legend image post style={sharp plot, silver}, mark options={beige}

\addplot可以为标记和线条定义颜色。

相关内容