
Quiere colocar la leyenda en la esquina superior izquierda del gráfico de barras. A continuación está mi código
\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}
Respuesta1
Para colocar la leyenda en la esquina superior izquierda de su gráfico, debe eliminarla legend columns = -1
de la lista de opciones del axis
entorno para que las tres entradas de la leyenda se impriman una debajo de la otra. Puedes posicionar la leyenda usando at = {(0,1)}, anchor = north west
. Finalmente, solía legend cell align = left
tener las entradas de la leyenda alineadas a la izquierda.
Aparte de eso, tal vez podrías limpiar algunas opciones y estilos que aplicas o defines, ya que no todos son realmente necesarios aquí:
\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}