pgfplots-堆积条形图的图例显示不正确

pgfplots-堆积条形图的图例显示不正确

我无法正确格式化图例。这是我的代码:

\begin{figure}[H]
\centering
\begin{tikzpicture}
\pgfplotstableread{
x Order                               m0  m132  m3  m460  m6
0 gtx780-cb-s 0.57615 0.627627 0.394365 0.811266 1.05492
1 gtx780-mu-s 0.141668 0.315022 0.145579 0.202026 0.253015
4 gtx780-cb-d 3.784450 10.841200 5.527380 5.881210 8.342070
5 gtx780-mu-d 0.279953 0.918595 0.340272 0.471707 0.422749
8 k40-cb-s 0.695936 0.831193 0.476254 1.05996 1.26672
9 k40-mu-s 0.193261 0.434555 0.195305 0.303927 0.338011
12 k40-cb-d 1.004380 2.401120 1.314340 1.560650 2.067400
13 k40-mu-d 0.377453 1.200990 0.377581 0.598974 0.674917
}\datatable

\begin{axis}[
    x post scale=\linewidth/\axisdefaultwidth,
    xlabel=Time {[}ms{]},
    ytick=data,
    yticklabels from table={\datatable}{Order},
    xbar stacked,
    xmin=0,
    ymax=15,
    legend style={area legend,at={(0.5,-0.3)},anchor=north,legend columns=-1},
]

\addplot table [x expr={4 * \thisrow{m0}}, y=x] {\datatable};
\addplot table [x=m132, y=x] {\datatable};
\addplot table [x=m3, y=x] {\datatable};
\addplot table [x=m460, y=x] {\datatable};
\addplot table [x=m6, y=x] {\datatable};

\addlegendimage{blue,sharp plot}
\addlegendimage{red,sharp plot}

\draw[blue] (axis cs:1.77394776117234,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1.77394776117234,\pgfkeysvalueof{/pgfplots/xmax});
\draw[red] (axis cs:2.12873731340681,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2.12873731340681,\pgfkeysvalueof{/pgfplots/xmax});

\legend{M0,M132,M3,M460,M6,line1,line2};
\end{axis}
\end{tikzpicture}

\end{figure}

我得到的输出是: 屏幕

如您所见,图例的格式不太好。我想要为条形图设置一些彩色区域,为添加到图中的两条线设置两条线。另外,如何将线条置于条形图之上?

谢谢!

答案1

  1. 作为休斯提到他的评论,将该选项添加area styleaxis环境中可以为图例提供所需的格式。

  2. 在添加图表之前先画出线条。

代码:

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}

\begin{figure}[H]
\centering
\begin{tikzpicture}
\pgfplotstableread{
x Order                               m0  m132  m3  m460  m6
0 gtx780-cb-s 0.57615 0.627627 0.394365 0.811266 1.05492
1 gtx780-mu-s 0.141668 0.315022 0.145579 0.202026 0.253015
4 gtx780-cb-d 3.784450 10.841200 5.527380 5.881210 8.342070
5 gtx780-mu-d 0.279953 0.918595 0.340272 0.471707 0.422749
8 k40-cb-s 0.695936 0.831193 0.476254 1.05996 1.26672
9 k40-mu-s 0.193261 0.434555 0.195305 0.303927 0.338011
12 k40-cb-d 1.004380 2.401120 1.314340 1.560650 2.067400
13 k40-mu-d 0.377453 1.200990 0.377581 0.598974 0.674917
}\datatable

\begin{axis}[
    x post scale=\linewidth/\axisdefaultwidth,
    xlabel=Time {[}ms{]},
    ytick=data,
    yticklabels from table={\datatable}{Order},
    xbar stacked,
    xmin=0,
    ymax=15,
    area style,
    legend style={area legend,at={(0.5,-0.3)},anchor=north,legend columns=-1},
]

\draw[blue] (axis cs:1.77394776117234,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1.77394776117234,\pgfkeysvalueof{/pgfplots/xmax});
\draw[red] (axis cs:2.12873731340681,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:2.12873731340681,\pgfkeysvalueof{/pgfplots/xmax});

\addplot table [x expr={4 * \thisrow{m0}}, y=x] {\datatable};
\addplot table [x=m132, y=x] {\datatable};
\addplot table [x=m3, y=x] {\datatable};
\addplot table [x=m460, y=x] {\datatable};
\addplot table [x=m6, y=x] {\datatable};

\addlegendimage{blue,sharp plot}
\addlegendimage{red,sharp plot}


\legend{M0,M132,M3,M460,M6,line1,line2};
\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}

在此处输入图片描述

相关内容