
以下程式碼繪製帶有數字標籤的長條圖。我希望圖例中的條目是相反的,即頂部有一個帶有標籤“二”的灰色框,下面有一個帶有標籤“一”的白色框。我怎樣才能做到這一點?我很可能會遺漏一些明顯的東西。編輯:繼續閱讀使用 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
傑克的建議,它解決了我原來的問題。
\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
正如傑克已經說過的那樣他的回答另一種方法是使用該\legend
命令。這樣做的優點是不需要forget plot
在命令上使用\addplot
,除了不將圖添加到圖例之外,還有一些其他(可能/有時不需要的)“副作用”,例如不推進cycle list
.
要使用命令忽略\addplot
s \legend
,只需為它們寫入空標籤即可。
(順便說一句:當同時給出ie\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}