
Ich möchte die Legende in der oberen linken Ecke des Balkendiagramms platzieren. Nachfolgend mein Code
\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}
Antwort1
Um die Legende in der oberen linken Ecke Ihres Diagramms zu platzieren, sollten Sie legend columns = -1
aus der Optionsliste der axis
Umgebung entfernen, dass die drei Legendeneinträge untereinander gedruckt werden. Sie können die Legende mit positionieren at = {(0,1)}, anchor = north west
. Schließlich würde ich verwenden, legend cell align = left
um die Legendeneinträge linksbündig auszurichten.
Abgesehen davon könntest du vielleicht einige Optionen und Stile, die du anwendest oder definierst, bereinigen, da nicht alle davon hier wirklich notwendig sind:
\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}