4

I have a code like below, but I am not sure why its legends become all blue and with a similar sign. Would you please help me to resolve this problem?

\documentclass{article}

\usepackage{pgfplots}
\usepackage{graphicx}

\begin{document}



\begin{figure}[h!]
    \centering
    \subfloat[SM]{
        \begin{tikzpicture}[scale=0.75, transform shape]
            \begin{axis}[
                title={ },
                xlabel={Instances},
                ylabel={},
                xmin=0, xmax=52,
                ymin=0, ymax=1.8,
                xtick={0,1,6,11,16,21,26,31,36,41,46,50},
                xticklabels={,1,6,11,16,21,26,31,36,41,46,50},
                ytick={0,0.20,0.40,0.60,0.80,1.00,1.20,1.40,1.60,1.80},
                yticklabels={0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,},
                legend pos=outer north east,
                ymajorgrids=true,
                grid style=dashed,
            ]
            
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (1,0)(2,0)(3,0.37)(4,0.4)
                };
                
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (11,1.5)(12,1.78)(13,0.57)
                };
                
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (21,1.5)(22,0.86)(23,0.76)
                };
                
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (31,1.67)(32,0.82)
                };
                
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (41,0)(42,0)
                };
                \addlegendentry{MP}
                
            \addplot[
                color=red,
                mark=asterisk,
                ]
                coordinates {
                (1,0)(2,0)(3,0.58)(4,0)(5,0.66)(6,0)(7,0.47)(8,0.42)(9,0.65)(10,0.68)(11,1.14)(12,1)(13,0.93)(14,0.67)(15,0.55)(16,0.4)(17,0.74)(18,0.8)(19,0.43)(20,0.95)(21,0.8)(22,0.25)(23,0.92)(24,0.52)(25,0.42)(26,1.01)(27,0.67)(28,0.67)(29,0.74)(30,0.61)(31,1.56)(32,0)(33,0.51)(34,0.56)(35,0.81)(36,0.76)(37,0.97)(38,0.86)(39,0.56)(40,0.88)(41,1)(42,0.32)(43,0.46)(44,0)(45,0.24)(46,0.91)(47,0.82)(48,0.76)(49,0.68)(50,0.89)
                };
                \addlegendentry{ACO}
                
            \addplot[
                color=black,
                mark=triangle,
                ]
                coordinates {
                (1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)(9,0)(10,0.59)(11,1.28)(12,1.32)(13,0)(14,0)(15,0.5)(16,0)(17,0)(18,0.19)(19,0.54)(20,0.63)(21,1.1)(22,0)(23,0)(24,0.02)(25,0)(26,0.54)(27,0.59)(28,0.6)(29,0.43)(30,0.53)(31,1.03)(32,1.25)(33,0)(34,0.42)(35,0.22)(36,0.39)(37,0.61)(38,0.9)(39,0.52)(40,0.69)(41,0)(42,0)(43,0.6)(44,0.35)(45,0.66)(46,0.4)(47,0.73)(48,0.6)(49,0.65)(50,0.8)
                };
                \addlegendentry{HT}

            \end{axis}
        \end{tikzpicture}
    }
\end{figure}


\end{document}
1
  • It was a part of my code. Fron now on, I will try to make sure that everything is also ok with the simplified code that I would like to post on here. Thansk.
    – mdslt
    May 3, 2021 at 17:15

2 Answers 2

8

\addlegendentry does not correspond to the \addplot immediately preceding it. The way it works is that legend entry number N corresponds to plot number N. To fix this add forget plot to the first four \addplots. This means that plot is not considered for the legend and cycle lists. So for example,

        \addplot[
            color=blue,
            mark=square,
            forget plot % <-- this is new
            ]
            coordinates {
            (1,0)(2,0)(3,0.37)(4,0.4)
            };
            

Complete example, where I removed the figure and subfloat because they're irrelevant for the problem, and you didn't include the package defining subfloat:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
        \begin{tikzpicture}[scale=0.75, transform shape]
            \begin{axis}[
                title={ },
                xlabel={Instances},
                ylabel={},
                xmin=0, xmax=52,
                ymin=0, ymax=1.8,
                xtick={0,1,6,11,16,21,26,31,36,41,46,50},
                xticklabels={,1,6,11,16,21,26,31,36,41,46,50},
                ytick={0,0.20,0.40,0.60,0.80,1.00,1.20,1.40,1.60,1.80},
                yticklabels={0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,},
                legend pos=outer north east,
                ymajorgrids=true,
                grid style=dashed,
            ]
            
            \addplot[
                color=blue,
                mark=square,forget plot
                ]
                coordinates {
                (1,0)(2,0)(3,0.37)(4,0.4)
                };
                
            \addplot[
                color=blue,
                mark=square,forget plot
                ]
                coordinates {
                (11,1.5)(12,1.78)(13,0.57)
                };
                
            \addplot[
                color=blue,
                mark=square,forget plot,
                ]
                coordinates {
                (21,1.5)(22,0.86)(23,0.76)
                };
                
            \addplot[
                color=blue,
                mark=square,forget plot,
                ]
                coordinates {
                (31,1.67)(32,0.82)
                };
                
            \addplot[
                color=blue,
                mark=square,
                ]
                coordinates {
                (41,0)(42,0)
                };
                \addlegendentry{MP}
                
            \addplot[
                color=red,
                mark=asterisk,
                ]
                coordinates {
                (1,0)(2,0)(3,0.58)(4,0)(5,0.66)(6,0)(7,0.47)(8,0.42)(9,0.65)(10,0.68)(11,1.14)(12,1)(13,0.93)(14,0.67)(15,0.55)(16,0.4)(17,0.74)(18,0.8)(19,0.43)(20,0.95)(21,0.8)(22,0.25)(23,0.92)(24,0.52)(25,0.42)(26,1.01)(27,0.67)(28,0.67)(29,0.74)(30,0.61)(31,1.56)(32,0)(33,0.51)(34,0.56)(35,0.81)(36,0.76)(37,0.97)(38,0.86)(39,0.56)(40,0.88)(41,1)(42,0.32)(43,0.46)(44,0)(45,0.24)(46,0.91)(47,0.82)(48,0.76)(49,0.68)(50,0.89)
                };
                \addlegendentry{ACO}
                
            \addplot[
                color=black,
                mark=triangle,
                ]
                coordinates {
                (1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)(9,0)(10,0.59)(11,1.28)(12,1.32)(13,0)(14,0)(15,0.5)(16,0)(17,0)(18,0.19)(19,0.54)(20,0.63)(21,1.1)(22,0)(23,0)(24,0.02)(25,0)(26,0.54)(27,0.59)(28,0.6)(29,0.43)(30,0.53)(31,1.03)(32,1.25)(33,0)(34,0.42)(35,0.22)(36,0.39)(37,0.61)(38,0.9)(39,0.52)(40,0.69)(41,0)(42,0)(43,0.6)(44,0.35)(45,0.66)(46,0.4)(47,0.73)(48,0.6)(49,0.65)(50,0.8)
                };
                \addlegendentry{HT}

            \end{axis}
        \end{tikzpicture}

\end{document}
5

The legend command uses the entries for each plot in a sequential order. Since your first five plots are blue marks (even if you don't label them as entries), also this number of entries will have blue marks. I simply changed the order of the plots to get the desired output:

enter image description here

\documentclass[varwidth=\maxdimen, border=5pt]{standalone}

\usepackage{pgfplots}
\usepackage{graphicx}

\begin{document}
    \begin{figure}[h!]
        \centering
            \begin{tikzpicture}[scale=0.75, transform shape]
                \begin{axis}[
                    title={ },
                    xlabel={Instances},
                    ylabel={},
                    xmin=0, xmax=52,
                    ymin=0, ymax=1.8,
                    xtick={0,1,6,11,16,21,26,31,36,41,46,50},
                    xticklabels={,1,6,11,16,21,26,31,36,41,46,50},
                    ytick={0,0.20,0.40,0.60,0.80,1.00,1.20,1.40,1.60,1.80},
                    yticklabels={0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,},
                    legend pos=outer north east,
                    ymajorgrids=true,
                    grid style=dashed,
                    ]
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (41,0)(42,0)
                    };
                    \addlegendentry{MP}
                    
                    \addplot[
                    color=red,
                    mark=asterisk,
                    ]
                    coordinates {
                        (1,0)(2,0)(3,0.58)(4,0)(5,0.66)(6,0)(7,0.47)(8,0.42)(9,0.65)(10,0.68)(11,1.14)(12,1)(13,0.93)(14,0.67)(15,0.55)(16,0.4)(17,0.74)(18,0.8)(19,0.43)(20,0.95)(21,0.8)(22,0.25)(23,0.92)(24,0.52)(25,0.42)(26,1.01)(27,0.67)(28,0.67)(29,0.74)(30,0.61)(31,1.56)(32,0)(33,0.51)(34,0.56)(35,0.81)(36,0.76)(37,0.97)(38,0.86)(39,0.56)(40,0.88)(41,1)(42,0.32)(43,0.46)(44,0)(45,0.24)(46,0.91)(47,0.82)(48,0.76)(49,0.68)(50,0.89)
                    };
                    \addlegendentry{ACO}
                    
                    \addplot[
                    color=black,
                    mark=triangle,
                    ]
                    coordinates {
                        (1,0)(2,0)(3,0)(4,0)(5,0)(6,0)(7,0)(8,0)(9,0)(10,0.59)(11,1.28)(12,1.32)(13,0)(14,0)(15,0.5)(16,0)(17,0)(18,0.19)(19,0.54)(20,0.63)(21,1.1)(22,0)(23,0)(24,0.02)(25,0)(26,0.54)(27,0.59)(28,0.6)(29,0.43)(30,0.53)(31,1.03)(32,1.25)(33,0)(34,0.42)(35,0.22)(36,0.39)(37,0.61)(38,0.9)(39,0.52)(40,0.69)(41,0)(42,0)(43,0.6)(44,0.35)(45,0.66)(46,0.4)(47,0.73)(48,0.6)(49,0.65)(50,0.8)
                    };
                    \addlegendentry{HT}
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (1,0)(2,0)(3,0.37)(4,0.4)
                    };
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (11,1.5)(12,1.78)(13,0.57)
                    };
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (21,1.5)(22,0.86)(23,0.76)
                    };
                    
                    \addplot[
                    color=blue,
                    mark=square,
                    ]
                    coordinates {
                        (31,1.67)(32,0.82)
                    };
                \end{axis}
            \end{tikzpicture}
    \end{figure}
\end{document}

You must log in to answer this question.

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