pgfplots:更改图例形状

pgfplots:更改图例形状

我有这个代码...

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

我只想要图例中的条目,因为它们的图标只是一个正方形。目前,它是一条线。如果我添加legend image post style={mark=square*},,它会显示一条线内的正方形。有什么方法可以让图例中只有一个正方形(没有线)。

答案1

使用

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

在此处输入图片描述

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

答案2

既然你说你想要一个正方形,我就补充说

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

要得到

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

在此处输入图片描述

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

你得到

在此处输入图片描述

请注意,如果绘制和填充颜色不同,这也同样有效。

相关内容