将图例放置在条形图的左上角

将图例放置在条形图的左上角

想要将图例放置在条形图的左上角。以下是我的代码

\documentclass[lettersize,journal]{IEEEtran}

\usepackage{pgfplots} 
\usetikzlibrary{patterns}

\pgfplotsset{ 
bar group size/.style 2 args={
    /pgf/bar shift={            
        -0.5*(#2*\pgfplotbarwidth + (#2-1)*\pgfkeysvalueof{/pgfplots/bar group skip})  + 
        (.5+#1)*\pgfplotbarwidth + #1*\pgfkeysvalueof{/pgfplots/bar group skip}},%
},
bar group skip/.initial=2pt,
errbars/.style={error bars/.cd, y dir=both, y explicit, error bar style ={thick},/pgfplots/.cd},
plot 0/.style={fill=blue, mark=none, postaction={pattern=dots},errbars},
plot 1/.style={fill=yellow, mark=none, postaction={pattern=north east lines},errbars},
plot 2/.style={fill=red, mark=none, postaction={pattern=horizontal lines},errbars},
plot 3/.style={fill=green, mark=none, postaction={pattern=vertical lines},errbars}
}

\begin{document}
\maketitle
\begin{figure}
\centering
\begin{tikzpicture}[font={\large}, scale=.43, transform shape]
\begin{axis} [
xtick={100, 200, 300, 400, 500},
major x tick style = transparent,
ybar = 2*\pgflinewidth,
    ybar = 3pt,
bar width = 12pt,
    every node near coord/.append style={rotate=90, anchor=west},
    width=11cm,
    height=8cm,
    ymajorgrids = true,
ylabel={Number of Intents},          
symbolic x coords={100, 200, 300, 400, 500},
scaled y ticks = false,
    enlarge x limits = 0.15,
    enlarge y limits={value=0.15,upper},
ymin = 0,
minor y tick num=5,     
tick label style={font=\large},
legend style={at={(.5,-0.12)}, anchor=north, /tikz/every even column/.append style={column sep=.2cm}},      
legend columns = -1     
]
\addplot [fill=cyan, mark=none, postaction={pattern=grid},error bars/.cd, y dir=both, y explicit] coordinates {  
    (100, 7.7) +- (2.920616373, 2.920616373)
(200, 16.08) +- (3.762127058, 3.762127058)
    (300, 23.24) +- (4.254691528, 4.254691528)      
(400, 29.72) +- (4.90322343, 4.90322343)
    (500, 38.02) +- (5.285792277, 5.285792277)
};
\addplot[fill=green, mark=none, postaction={pattern=north west lines},error bars/.cd, y dir=both, y explicit] coordinates { 
    (100, 92.3) +- (2.920616373, 2.920616373)       
(200, 183.92) +- (3.762127058, 3.762127058)
    (300, 276.76) +- (4.254691528, 4.254691528)    
(400, 370.28) +- (4.90322343, 4.90322343)
    (500, 461.98) +- (5.285792277, 5.285792277)
};
\addplot [fill=yellow, mark=none, postaction={pattern=north east lines}] coordinates {  
(100, 100)     
(200, 200)
(300, 300)     
(400, 400)
(500, 500)
};
\legend{Rejected, Accepted, Accepted with Negotiation}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

答案1

要将图例放置在图表左上角的 中,您应该legend columns = -1从环境的选项列表中删除axis,以便将三个图例条目打印在彼此下方。您可以使用 来定位图例at = {(0,1)}, anchor = north west。最后,我将使用legend cell align = left使图例条目左对齐。

除此之外,您也许可以清理一些您应用或定义的选项和样式,因为它们并不是全部都是真正必要的:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}

\pgfplotsset{ 
    bar group size/.style 2 args={
        /pgf/bar shift={            
            -0.5*(#2*\pgfplotbarwidth + (#2-1)*\pgfkeysvalueof{/pgfplots/bar group skip})  + 
            (.5+#1)*\pgfplotbarwidth + #1*\pgfkeysvalueof{/pgfplots/bar group skip}
        },
    },
    bar group skip/.initial=2pt,
}

\begin{document}
\begin{tikzpicture}[font={\large}, scale=.43, transform shape]
\begin{axis} [
    xtick={100, 200, 300, 400, 500},
    major x tick style=transparent,
    ybar=3pt,
    bar width=12pt,
    width=11cm,
    height=8cm,
    ymajorgrids=true,
    ylabel={Number of Intents},          
    enlarge x limits=0.15,
    enlarge y limits={value=0.15, upper},
    ymin = 0,
    minor y tick num=5,     
    legend style={at={(0,1)}, anchor=north west, legend cell align=left},      
]
\addplot[fill=cyan, mark=none, postaction={pattern=grid}, error bars/.cd, y dir=both, y explicit] coordinates {  
    (100, 7.7) +- (2.920616373, 2.920616373)
    (200, 16.08) +- (3.762127058, 3.762127058)
    (300, 23.24) +- (4.254691528, 4.254691528)      
    (400, 29.72) +- (4.90322343, 4.90322343)
    (500, 38.02) +- (5.285792277, 5.285792277)
};
\addplot[fill=green, mark=none, postaction={pattern=north west lines}, error bars/.cd, y dir=both, y explicit] coordinates { 
    (100, 92.3) +- (2.920616373, 2.920616373)       
    (200, 183.92) +- (3.762127058, 3.762127058)
    (300, 276.76) +- (4.254691528, 4.254691528)    
    (400, 370.28) +- (4.90322343, 4.90322343)
    (500, 461.98) +- (5.285792277, 5.285792277)
};
\addplot [fill=yellow, mark=none, postaction={pattern=north east lines}] coordinates {  
    (100, 100)     
    (200, 200)
    (300, 300)     
    (400, 400)
    (500, 500)
};
\legend{Rejected, Accepted, Accepted with Negotiation}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容