
다음 코드는 숫자 레이블이 있는 막대 차트를 그립니다. 범례의 항목을 반대 방향으로 설정하고 싶습니다. 즉, 상단에 "two"라는 레이블이 있는 회색 상자가 있고 아래에 "one"이라는 레이블이 있는 흰색 상자가 있기를 원합니다. 어떻게 해야 합니까? 나는 분명한 것을 놓쳤을 수도 있습니다.편집: pgfplots 1.14를 사용하는 버전을 읽어보세요.
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
symbolic x coords={A,B,C},
xtick=data,
xticklabel style={align=center},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords align={vertical},
nodes near coords align={anchor=north},%Move values in bar
totals/.style={nodes near coords align={anchor=south}},
]
\addplot [fill=white] coordinates {
({A},24)
({B},16)
({C},11)};
\addlegendentry{one}
\addplot [fill=lightgray,point meta=explicit] coordinates {
({A},53)[53]
({B},47)[47]
({C},33)[33]};
\addlegendentry{two}
\addplot[totals] coordinates {
({A},0)
({B},0)
({C},0)};
\legend{one,two}
\end{axis}
\end{tikzpicture}
\end{document}
편집하다:나중에 오시는 분들을 위해 다음 코드는 내 원래 게시물과 거의 동일한 레이아웃을 갖지만 pgfplots 버전 1.14와 호환됩니다(참조tex.stackexchange.com/a/162389/95441). reverse legend
내 원래 문제를 해결하는 Jake의 제안을 포함했습니다 .
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
show sum on top/.style={
/pgfplots/scatter/@post marker code/.append code={%
\node[
at={(normalized axis cs:%
\pgfkeysvalueof{/data point/x},%
\pgfkeysvalueof{/data point/y})%
},
anchor=south,
]
{\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
},
},
}
\begin{axis}[
reverse legend,
ybar stacked,
ymin=0,
ymax=90,
symbolic x coords={A,B,C},
xtick=data,
xticklabel style={align=center},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
]
\addplot [fill=white] coordinates {
({A},24)
({B},16)
({C},11)};
\addlegendentry{one}
\addplot [fill=lightgray,show sum on top] coordinates {
({A},53)
({B},47)
({C},33)};
\addlegendentry{two}
\legend{one,two}
\end{axis}
\end{tikzpicture}
\end{document}
답변1
reverse legend
에서 설정 axis options
하고 범례에 표시하지 않으려는 명령을 forget plot
추가 합니다. 또한 두 명령 또는 명령 \addplot
중 하나만 필요 하고 둘 다 필요하지는 않습니다.\addlegendentry
\legend
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
reverse legend,
symbolic x coords={A,B,C},
xtick=data,
xticklabel style={align=center},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords align={vertical},
nodes near coords align={anchor=north},%Move values in bar
totals/.style={nodes near coords align={anchor=south}},
]
\addplot [fill=white] coordinates {
({A},24)
({B},16)
({C},11)};
\addlegendentry{one}
\addplot [fill=lightgray,point meta=explicit] coordinates {
({A},53)[53]
({B},47)[47]
({C},33)[33]};
\addlegendentry{two}
\addplot[totals, forget plot] coordinates {
({A},0)
({B},0)
({C},0)};
\end{axis}
\end{tikzpicture}
\end{document}
답변2
Jake가 이미 언급했듯이그의 대답대안은 \legend
명령을 사용하는 것입니다. 이것은 명령 forget plot
에 사용할 필요가 없다는 장점이 있으며 \addplot
, 범례에 플롯을 추가하지 않는 것 외에도 cycle list
.
\addplot
명령 으로 s를 무시하려면 \legend
빈 레이블을 작성하면 됩니다.
(그런데: , 와 \addlegendentry
가 모두 \legend
주어지면 \legend
"승리"합니다.)
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
reverse legend,
symbolic x coords={A,B,C},
xtick=data,
nodes near coords,
nodes near coords align={anchor=north},%Move values in bar
totals/.style={
nodes near coords align={anchor=south},
red, % <-- added, to distinguish it from "one"
},
]
\addplot [fill=white] coordinates {
(A,24) (B,16) (C,11)
};
\addplot [fill=lightgray,point meta=explicit] coordinates {
(A,53)[53] (B,47)[47] (C,33)[33]
};
\addplot [totals] coordinates {
(A,0) (B,0) (C,0)
};
% give all the `\addplot's that should not be shown in the legend
% an empty legend entry
% (here "three" is empty, because there is a comma after the last entry)
\legend{
one,
two,
}
% % perhaps this one easier to understand
% % (here "two" isn't shown in the legend
% \legend{
% one,
% ,
% three
% }
\end{axis}
\end{tikzpicture}
\end{document}