# Problem with legend in pgfplots

I want to make plot with fill area. I try this code

\documentclass[a4paper,12pt]{report}

\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}

\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymin=2.56286,
height=9cm,
width=15cm,
grid=major,
]
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}

\end{document}


And I see this resut: I have problem with legend. A see line from first plot without marks. How can I fix it?

Update. If I use \closedcycle lines appear on a cycle. They look like a continuation of the main line. And if I use If I use negative numbers, the chart draws a line at zero.

If I use second solution (changing order) and try add two (four of the present) legend draws the wrong line.

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymax=42,
xmax=6.5,
height=9cm,
width=15cm,
grid=major,
axis lines=left,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\addplot[color=red, mark=*,mark options={red,opacity = 1, fill=white,thick},thick] coordinates {
(-2,20)
(2.,3)
(5,10)
};
\addplot[color=red, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-2,2.56286)
(-2,20)
(2.,3)
(5,10)
(5,2.56286)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}


You don't have to use \addplot two times. Use \closedcycle instead.

\documentclass[a4paper,12pt]{report}

\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}

\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymin=2.56286,
height=9cm,
width=15cm,
grid=major,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},fill,fill opacity = 0.5,thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
}\closedcycle;
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}

\end{document}


If you are still tied to using addplot two times, change the order:

\documentclass[a4paper,12pt]{report}

\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}

\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymin=2.56286,
height=9cm,
width=15cm,
grid=major,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}

\end{document}


Edit: for the updated

\closedcycle is indeed a cycle, and the behaviour is expected. For the second query again use the proper order:

\documentclass[a4paper,12pt]{report}

\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}

\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymax=42,
xmax=6.5,
height=9cm,
width=15cm,
grid=major,
axis lines=left,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addplot[color=red, mark=*,mark options={red,opacity = 1, fill=white,thick},thick] coordinates {
(-2,20)
(2.,3)
(5,10)
};
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\addplot[color=red, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-2,2.56286)
(-2,20)
(2.,3)
(5,10)
(5,2.56286)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}

\end{document}


• In first solution lines appear on a cycle. They look like a continuation of the main line. And if I use If I use negative numbers, the chart draws a line at zero (picture 2 in update post). If I use second solution and try add two (four of the present) legend draws the wrong line (picture 3 in update post). Can addplot to ignore the addition of plot to a legend? – Harrix Oct 29 '13 at 15:54
• @user1680906 \closedcycle is doing things right. What do you want exactly? And for the second again change the order. See the update. – user11232 Oct 29 '13 at 22:18
• Thank you! It works by changing the order. Found also that the use forget plot in \addplot can not change the order. – Harrix Oct 31 '13 at 14:12
• @Harrix Oh yes, I forgot that option. Glad you discovered it. :-) – user11232 Oct 31 '13 at 15:19