在直方图背景中添加区域

在直方图背景中添加区域

我希望我的直方图在背景中有区域(带有名称),以便比较每个条形的值与某些间隔。这是我想要达到的结果:

在此处输入图片描述

这是我目前所达到的:

在此处输入图片描述

2 个颜色变化分别设置为 0.35 和 0.62。我使用的代码如下:

\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,
        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]
        \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

这是一个选项:

\documentclass[border=5pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}

\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{document}
    \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]
   \path
     coordinate (a) at (axis cs:7-Pa-S,0.35)
     coordinate (b) at (axis cs:7-Pa-S,0.62);
    \fill[pink]
      (rel axis cs:0,0) rectangle ({rel axis cs:1,0.35}|-a);
    \fill[green!70!black!70]
      ({rel axis cs:0,0.35}|-a) rectangle ({rel axis cs:1,0.62}|-b);
    \fill[blue!70!black!50]
      ({rel axis cs:0,0.62}|-b) rectangle (rel axis cs:1,1);
    \path 
    node[anchor=east] at (rel axis cs:1,0.73) {Clay}  
    node[anchor=east] at (rel axis cs:1,0.4) {Loam}  
    node[anchor=east] at (rel axis cs:1,0.2) {Sand};  
        \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}

在此处输入图片描述

相关内容