tikz 条形图中的重叠标签

tikz 条形图中的重叠标签

我对条形图中节点的位置和大小有疑问。我在代码中将大小从\小的\微小的

every node near coord/.append style={font=\tiny}

但数字仍然大于条宽,并且相互重叠。我尝试在线查找类似问题,但找不到任何解决方案。

是否可以通过手动放置或改变节点大小来提高可读性?

感谢您的帮助!

\documentclass[12pt]{book}
\usepackage{pgfplots}
\newlength\figureheight 
\newlength\figurewidth
\begin{document}
\begin{figure}[H]
\centering
\definecolor{myblue}{HTML}{0058a2}
\definecolor{mygreen}{HTML}{13B508}
\definecolor{myred}{HTML}{FF0000}
\definecolor{myskyblue}{HTML}{00C9FF}
\definecolor{mybrown}{HTML}{9C7E05}
\definecolor{myorange}{HTML}{E87E05}
 \setlength\figureheight{9cm} 
\setlength\figurewidth{\textwidth}
    \begin{tikzpicture}
\begin{axis}[
    axis lines=left,
color=black,
width=\figurewidth,
    height=\figureheight,
    bar width=6pt,
ybar, 
enlargelimits=0.15,
legend style={at={(0.5,-0.15)},
  anchor=north,legend columns=-1},
ylabel={\%},
symbolic x coords={2008,2009,2010,2011,2012,2013},
xtick=data,
nodes near coords,
    every node near coord/.append style={font=\tiny},
nodes near coords align={vertical},
    nodes/.style={font=\footnotesize}
]

\begin{scope}[gray]
        \draw[gray] ({rel axis cs:1,0}|-{axis cs:2009,0}) -- ({rel axis cs:0,0}|-{axis cs:2009,0});
\end{scope}

\addplot[fill=myorange]
    coordinates{
    (2008,0)
    (2009,-12.6)
    (2010,8.9)
    (2011,1.7)
    (2012,0.4)
    (2013,1)
    };
  \addplot[fill=myblue]
    coordinates{   
    (2008,-3.2)
    (2009,-10)
    (2010,7)
    (2011,-6.3)
    (2012,-0.6)
    (2013,-0.6)
    };
 \addplot[fill=myred]
    coordinates{  
    (2008,4.7)
    (2009,-8.3)
    (2010,14.9)
    (2011,2.7)
    (2012,4.8)
    (2013,1.1)
    };
     \addplot[fill=myskyblue]
    coordinates{   
    (2008,1.8)
    (2009,-12.4)
    (2010,5.9)
    (2011,-5.4)
    (2012,-3.1)
    (2013,-3.1)
    };
  \addplot[fill=mygreen]
    coordinates{  
    (2008,-3.1)
    (2009,-12.2)
    (2010,6.6)
    (2011,-5.5)
    (2012,-1.8)
    (2013,-2.1)
    };
 \addplot[fill=mybrown]
    coordinates{     
    (2008,4.5)
    (2009,-11.3)
    (2010,11.7)
    (2011,-5.4)
    (2012,-4.3)
    (2013,-3.3)
   };
\legend{A,B,C,D,E,F}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

条形图

答案1

green bar (E)在这个提议中,解决方案是通过添加如下所示的本地选项来向下移动与之关联的重叠标签,并对yshift坐标=1 或 3 或 4 或 5 进行条件测试,因为当前坐标索引存储在宏中\coordindex(从 0,1,2...开始):此外,myredmybrown也有一个单一的移动。

 nodes near coords,
 every node near coord/.append style={
 yshift={\ifnum
 \ifnum\coordindex=1 1 
 \else\ifnum\coordindex=3 1
 \else\ifnum\coordindex=4 1
 \else\ifnum\coordindex=5 1 \else 0\fi\fi\fi\fi
                    =1 %
                    -2.5ex  % negative value for moving down and vice versa
                  \else 
                     0pt    % default
                  \fi}},

如果需要更多的起伏,也可以通过添加/追加来应用同样的想法

 every node near coord/.append style={
 yshift={conditional check}}

参见材料如何编写条件 ifnum以供进一步参考。

在此处输入图片描述

代码

\documentclass[12pt]{book}
\usepackage{pgfplots}
\newlength\figureheight 
\newlength\figurewidth
\begin{document}
\begin{figure}[H]
\centering
\definecolor{myblue}{HTML}{0058a2}
\definecolor{mygreen}{HTML}{13B508}
\definecolor{myred}{HTML}{FF0000}
\definecolor{myskyblue}{HTML}{00C9FF}
\definecolor{mybrown}{HTML}{9C7E05}
\definecolor{myorange}{HTML}{E87E05}
 \setlength\figureheight{9cm} 
\setlength\figurewidth{\textwidth}
    \begin{tikzpicture}
\begin{axis}[
    axis lines=left,
color=black,
width=\figurewidth,
    height=\figureheight,
    bar width=6pt,
ybar, 
enlargelimits=0.15,
legend style={at={(0.5,-0.15)},
  anchor=north,legend columns=-1},
ylabel={\%},
symbolic x coords={2008,2009,2010,2011,2012,2013},
xtick=data,
nodes near coords,
    every node near coord/.append style={font=\tiny},
nodes near coords align={vertical},
    nodes/.style={font=\footnotesize}
]

\begin{scope}[gray]
        \draw[gray] ({rel axis cs:1,0}|-{axis cs:2009,0}) -- ({rel axis cs:0,0}|-{axis cs:2009,0});
\end{scope}

\addplot[fill=myorange]
    coordinates{
    (2008,0)
    (2009,-12.6)
    (2010,8.9)
    (2011,1.7)
    (2012,0.4)
    (2013,1)
    };
  \addplot[fill=myblue]
    coordinates{   
    (2008,-3.2)
    (2009,-10)
    (2010,7)
    (2011,-6.3)
    (2012,-0.6)
    (2013,-0.6)
    };
 \addplot[fill=myred,
   nodes near coords,
    every node near coord/.append style={
        yshift={\ifnum\coordindex=1 -6ex
                    \else  0pt
                    \fi
                   }},
]
    coordinates{  
    (2008,4.7)
    (2009,-8.3)
    (2010,14.9)
    (2011,2.7)
    (2012,4.8)
    (2013,1.1)
    };
     \addplot[fill=myskyblue]
    coordinates{   
    (2008,1.8)
    (2009,-12.4)
    (2010,5.9)
    (2011,-5.4)
    (2012,-3.1)
    (2013,-3.1)
    };
  \addplot[fill=mygreen,
   nodes near coords,
    every node near coord/.append style={
        yshift={\ifnum
\ifnum\coordindex=1 1 
\else\ifnum\coordindex=3 1
\else\ifnum\coordindex=4 1
\else\ifnum\coordindex=5 1 \else 0\fi\fi\fi\fi
                    =1 %
                    -2.5ex
                  \else 
                     0pt
                  \fi}},
]
    coordinates{  
    (2008,-3.1)
    (2009,-12.2)
    (2010,6.6)
    (2011,-5.5)
    (2012,-1.8)
    (2013,-2.1)
    };

 \addplot[fill=mybrown,  nodes near coords,
    every node near coord/.append style={
        yshift={\ifnum\coordindex=4 -1ex
                    \else  0pt
                    \fi
                   }
}]
    coordinates{     
    (2008,4.5)
    (2009,-11.3)
    (2010,11.7)
    (2011,-5.4)
    (2012,-4.3)
    (2013,-3.3)
   };
\legend{A,B,C,D,E,F}
\end{axis}
\end{tikzpicture}
\end{figure}

\结束{文档}

相关内容