2

I have this code...

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotscreateplotcyclelist{colour-list}{
red,style={fill=red}\\
orange,style={fill=orange}\\
yellow,style={fill=yellow}\\
green,style={fill=green}\\
blue,style={fill=blue}\\
}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,stage1,stage2,stage3,stage4,stage5
2017-11-20,1,2,3,4,5
2017-11-21,2,3,4,5,6
2017-11-22,3,4,5,6,7
2017-11-23,4,5,6,7,8
2017-11-24,5,6,7,8,9
\end{filecontents*}
\begin{document}
\begin{figure}[H]
\hspace{-5em}
\fbox{
    \begin{tikzpicture}
    \begin{axis}[
    table/col sep=comma,
    width=16.5cm,
    height=8cm,
    title=Chart Title,
    axis lines=left,
    axis line style={draw=none},
    date coordinates in=x,
    xtick=data,
    xticklabel=\day/\month/\year,
    ytick style={draw=none},
    ymajorgrids=true,
    ytick distance=5,
    ymax= 40,
    tick align=outside,
    stack plots=y,
    legend columns = 5,
    legend style={at={(0.5,-0.15)}, anchor=north, style={draw=none}},
    cycle list name=colour-list,
    ]
\addplot table[x=date,y=stage1]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage2]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage3]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage4]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage5]{data.csv}
\closedcycle;
\legend{Stage 1, Stage 2, Stage 3, Stage 4, Stage 5}
\end{axis}
\end{tikzpicture}
}
\end{figure}
\end{document}

I want only the entries in the legend, for their icon to be just a square. Currently, it's a line. If I add legend image post style={mark=square*},, it shows a square within a line. Any way to get just a square in the legend only (no line).

1

Use

legend image post style={only marks, mark=square*},

enter image description here

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotscreateplotcyclelist{colour-list}{
red,style={fill=red}\\
orange,style={fill=orange}\\
yellow,style={fill=yellow}\\
green,style={fill=green}\\
blue,style={fill=blue}\\
}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,stage1,stage2,stage3,stage4,stage5
2017-11-20,1,2,3,4,5
2017-11-21,2,3,4,5,6
2017-11-22,3,4,5,6,7
2017-11-23,4,5,6,7,8
2017-11-24,5,6,7,8,9
\end{filecontents*}
\begin{document}
\begin{figure}[H]
\hspace{-5em}
\fbox{
    \begin{tikzpicture}
    \begin{axis}[
legend image post style={only marks, mark=square*},
    table/col sep=comma,
    width=16.5cm,
    height=8cm,
    title=Chart Title,
    axis lines=left,
    axis line style={draw=none},
    date coordinates in=x,
    xtick=data,
    xticklabel=\day/\month/\year,
    ytick style={draw=none},
    ymajorgrids=true,
    ytick distance=5,
    ymax= 40,
    tick align=outside,
    stack plots=y,
    legend columns = 5,
    legend style={at={(0.5,-0.15)}, anchor=north, style={draw=none}},
    cycle list name=colour-list,
    ]
\addplot table[x=date,y=stage1]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage2]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage3]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage4]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage5]{data.csv}
\closedcycle;
\legend{Stage 1, Stage 2, Stage 3, Stage 4, Stage 5}
\end{axis}
\end{tikzpicture}
}
\end{figure}
\end{document}
1

Since you say you want a square, I added

legend image code/.code={
        \draw [#1] (0cm,-0.1cm) rectangle (0.2cm,0.1cm); },

to get

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotscreateplotcyclelist{colour-list}{
red,style={fill=red}\\
orange,style={fill=orange}\\
yellow,style={fill=yellow}\\
green,style={fill=green}\\
blue,style={fill=blue}\\
}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,stage1,stage2,stage3,stage4,stage5
2017-11-20,1,2,3,4,5
2017-11-21,2,3,4,5,6
2017-11-22,3,4,5,6,7
2017-11-23,4,5,6,7,8
2017-11-24,5,6,7,8,9
\end{filecontents*}
\begin{document}
\begin{figure}[H]
\hspace{-5em}
\fbox{
    \begin{tikzpicture}
    \begin{axis}[
    table/col sep=comma,
    width=16.5cm,
    height=8cm,
    title=Chart Title,
    axis lines=left,
    axis line style={draw=none},
    date coordinates in=x,
    xtick=data,
    xticklabel=\day/\month/\year,
    ytick style={draw=none},
    ymajorgrids=true,
    ytick distance=5,
    ymax= 40,
    tick align=outside,
    stack plots=y,
    legend columns = 5,
    legend image code/.code={
        \draw [#1] (0cm,-0.1cm) rectangle (0.2cm,0.1cm); },
    legend style={at={(0.5,-0.15)}, 
    anchor=north, style={draw=none}},
    cycle list name=colour-list,
    ]
\addplot table[x=date,y=stage1]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage2]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage3]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage4]{data.csv}
\closedcycle;
\addplot table[x=date,y=stage5]{data.csv}
\closedcycle;
\legend{Stage 1, Stage 2, Stage 3, Stage 4, Stage 5}
\end{axis}
\end{tikzpicture}
}
\end{figure}
\end{document}

enter image description here

With

legend image code/.code={
    \draw [#1] (0cm,-0.15cm) rectangle (0.3cm,0.15cm); }

you get

enter image description here

Note that this also works if the draw and fill colors differ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.