1

I want to change the color of the line in the legend image, because the line color in my plot is different to the mark color.

\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}

As you can see in the picture, i put lines in the legend image for plot E, but i cannot change the color of the plotline in the legend image. plot

yes.na
  • 39
  • 1
    I think you need to change the line color in the plot, and the legend will adopt that color automatically. Unless you want to change only the legend, but that would make it kind of pointless. – Alenanno Dec 15 '15 at 10:09
  • 1
    i do not want to change the color in the plot. i only want to adopt the color in the legend to the color in the plot – yes.na Dec 15 '15 at 10:25
  • You can even create a legend that has absolutely nothing to do with the plot. (That will confuse them!) See http://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture/148855?s=1|1.9433#148855 – John Kormylo Dec 15 '15 at 16:34

2 Answers2

1

I don't really understand why you would do what you're doing, but here's another approach. In the options to the E plot, add forget plot, so it's not included in the legend, and then add

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

As you see you can set the color of the markers separately from the line by having color in the mark options.

Also, for drawing the Silver line there is no need for the long sequence of \draw commands, just use an \addplot.

enter image description here

\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}
Torbjørn T.
  • 206,688
-1

I just solved it... If you add

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

to \addplot you can define a color for the mark and the line.

yes.na
  • 39