막대 그래프의 왼쪽 상단에 범례 배치

막대 그래프의 왼쪽 상단에 범례 배치

막대 그래프의 왼쪽 상단에 범례를 배치하고 싶습니다. 다음은 내 코드입니다

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

여기에 이미지 설명을 입력하세요

관련 정보