我正在使用这个 ybar 堆积图,但我无法将节点放置在靠近坐标的正确位置(不在某个东西之上、不在图表之外、不在其他条形图之上、不太远...可读!)。
我尝试过nodes near coords align={north}
(西等),它改变了位置,但我无法理解这个坐标系统!
我的代码:
\documentclass{article}
\usepackage{pgfplots} % to print charts
\pgfplotsset{compat=1.8}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
xbar legend,
% nodes near coords align={right},
legend pos=outer north east,
enlarge x limits={abs=1},
enlarge y limits=false,
bar width=.5,
% x axis
xtick={1,2},
xticklabels={Dots,Squares},
% y axis
ymin=0,
ylabel={Aliens founds},
]
\addplot table [
x=index,
y=blue,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=pink,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=gray,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\legend{First one,Second one,Third}
\end{axis}
\end{tikzpicture}
\caption{Caption placeholder}
\end{figure}
\end{document}
我的图表:
答案1
我的建议是更新您的pgfplots
软件包。从pgfplots
版本 1.9 开始,nodes near coords
效果会更好。将结果与版本 1.8 进行比较:
\documentclass{article}
\usepackage{pgfplots} % to print charts
\pgfplotsset{compat=1.8}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
every node near coord/.append style={font=\footnotesize},
xbar legend,
% nodes near coords align={right},
legend pos=outer north east,
enlarge x limits={abs=1},
enlarge y limits=false,
bar width=.5,
% x axis
xtick={1,2},
xticklabels={Dots,Squares},
% y axis
ymin=0,
ylabel={Aliens founds},
]
\addplot table [
x=index,
y=blue,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=pink,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=gray,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\legend{First one,Second one,Third}
\end{axis}
\end{tikzpicture}
\caption{Caption placeholder}
\end{figure}
\end{document}
以及 1.9 版或更新版本:
\documentclass{article}
\usepackage{pgfplots} % to print charts
\pgfplotsset{compat=1.9}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
every node near coord/.append style={font=\footnotesize},
xbar legend,
% nodes near coords align={right},
legend pos=outer north east,
enlarge x limits={abs=1},
enlarge y limits=false,
bar width=.5,
% x axis
xtick={1,2},
xticklabels={Dots,Squares},
% y axis
ymin=0,
ylabel={Aliens founds},
]
\addplot table [
x=index,
y=blue,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=pink,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=gray,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\legend{First one,Second one,Third}
\end{axis}
\end{tikzpicture}
\caption{Caption placeholder}
\end{figure}
\end{document}
如果无法更新,您可以尝试一些手动调整;例如:
every node near coord/.append style={xshift=-15pt,yshift=-3pt,anchor=east,font=\footnotesize},
得出
\documentclass{article}
\usepackage{pgfplots} % to print charts
\pgfplotsset{compat=1.8}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
nodes near coords,
every node near coord/.append style={xshift=-15pt,yshift=-3pt,anchor=east,font=\footnotesize},
xbar legend,
% nodes near coords align={right},
legend pos=outer north east,
enlarge x limits={abs=1},
enlarge y limits=false,
bar width=.5,
% x axis
xtick={1,2},
xticklabels={Dots,Squares},
% y axis
ymin=0,
ylabel={Aliens founds},
]
\addplot table [
x=index,
y=blue,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=pink,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\addplot table [
x=index,
y=gray,
] {
index blue pink gray
1 25 10 5
2 35 0 100
};
\legend{First one,Second one,Third}
\end{axis}
\end{tikzpicture}
\caption{Caption placeholder}
\end{figure}
\end{document}