文本超出 \axis

文本超出 \axis

我想将shift节点的文本移出axis区域,就像第一幅图那样。如果这很难实现,那么第二幅图中的解决方案也不错,其中将区域的右边缘移动,以便为文本留出足够的空间node

在此处输入图片描述

在此处输入图片描述

我使用的代码是这样的:

\begin{filecontents*}{WHCs.dat}
Sample  y   y_err
5-Pa-Pr 0.520   0.10
6-Pa-Pr 0.465   0.05
7-Pa-Pr 0.768   0.16
5-Pa-S  0.517   0.10
6-Pa-S  0.562   0.20
7-Pa-S  0.794   0.35
\end{filecontents*} 

    \begin{tikzpicture}
    \begin{axis}[
        ybar, ymin=0,
        bar width=15pt,
        x tick label style={rotate=45, anchor=east},
        major x tick style = transparent,
        ymajorgrids = true,
        xtick = data,
        axis on top,
        enlarge x limits=0.35,
        scaled y ticks = false,
        symbolic x coords={5-Pa-Pr, 6-Pa-Pr, 7-Pa-Pr, 5-Pa-S, 6-Pa-S, 7-Pa-S},
        ymajorgrids]
   \begin{scope}
    \fill[brown] (rel axis cs:0,0.355) rectangle (rel axis cs:1,0.365);
    \fill[green] (rel axis cs:0,0.495) rectangle (rel axis cs:1,0.505);
    \fill[red] (rel axis cs:0,0.555) rectangle (rel axis cs:1,0.565);
    \path 
    node[anchor=east] at (rel axis cs:1,0.36) {Sand}  
    node[anchor=east] at (rel axis cs:1,0.50) {Clay}  
    node[anchor=east] at (rel axis cs:1,0.56) {Silty loam};  
    \end{scope}      
        \addplot+[error bars/.cd, y dir=both,y explicit] table [x=Sample, y=y, y error=y_err] {WHCs.dat};
    \end{axis}
    \end{tikzpicture}

答案1

用来clip=false避免裁剪超出轴范围的内容。然后anchor=west代替anchor=east这些节点使用。

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{WHCs.dat}
Sample  y   y_err
5-Pa-Pr 0.520   0.10
6-Pa-Pr 0.465   0.05
7-Pa-Pr 0.768   0.16
5-Pa-S  0.517   0.10
6-Pa-S  0.562   0.20
7-Pa-S  0.794   0.35
\end{filecontents*}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        clip=false,
        ybar, ymin=0,
        bar width=15pt,
        x tick label style={rotate=45, anchor=east},
        major x tick style = transparent,
        ymajorgrids = true,
        xtick = data,
        axis on top,
        enlarge x limits=0.35,
        scaled y ticks = false,
        symbolic x coords={5-Pa-Pr, 6-Pa-Pr, 7-Pa-Pr, 5-Pa-S, 6-Pa-S, 7-Pa-S},
        ymajorgrids]
   \begin{scope}
    \fill[brown] (rel axis cs:0,0.355) rectangle (rel axis cs:1,0.365);
    \fill[green] (rel axis cs:0,0.495) rectangle (rel axis cs:1,0.505);
    \fill[red] (rel axis cs:0,0.555) rectangle (rel axis cs:1,0.565);
    \path
    node[anchor=west] at (rel axis cs:1,0.36) {Sand}
    node[anchor=west] at (rel axis cs:1,0.50) {Clay}
    node[anchor=west] at (rel axis cs:1,0.56) {Silty loam};
    \end{scope}
        \addplot+[error bars/.cd, y dir=both,y explicit] table [x=Sample, y=y, y error=y_err] {WHCs.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容