堆叠 ybar 间隔

堆叠 ybar 间隔

我正在尝试绘制 2 个堆叠在一起的条形图。我已成功绘制单个 ybar 图,如下所示:

\documentclass[11pt]{article}                                                    
\usepackage[top=2.54cm,bottom=2.54cm,left=2.75cm,right=2.75cm]{geometry}         
\usepackage{tikz}                                                                
\usetikzlibrary{calc}                                                            
\usepackage{pgfplots}                                                            
\usepgfplotslibrary{fillbetween}                                                 
\pgfplotsset{compat=1.17}                                                        
                                                                                 
\begin{filecontents}{data.txt}                                                   
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{filecontents}{data2.txt}                                                  
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{document}                                                                 
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} [                                                           
                height = \axisdefaultheight,                                     
                width = \textwidth,                                              
                xlabel = Channel,                                                
                ylabel = Counts,                                                 
                ymin = 0,                                                        
                xmin = -5,                                                       
                xmax = 260,                                                      
                xtick pos = left,                                                
                axis on top,                                                     
            ]                                                                    
            % Vertical divisions                              
            \path  (current axis.above origin) coordinate (T);                   
            \pgfplotsinvokeforeach{0,...,4}{                                     
                \draw[draw=black!6, name path=line#1]  (64*#1-0.5,0) -- (64*#1-0.5,0|-T);
            }                                                                    
            \path[name path=line5] (\pgfkeysvalueof{/pgfplots/xmax},0) -- (\pgfkeysvalueof{/pgfplots/xmax}, 0|-T);
            \pgfplotsinvokeforeach{0,2,4} {                                      
                \addplot [fill=black!6] fill between[of=line#1 and               
                line\the\numexpr#1+1\relax];                                     
            }                                                                    
                                                                                 
            \addplot[                                                            
                ybar interval,                                                   
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black!20] table {data.txt};                               
        \end{axis}                                                               
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\end{document}

哪一个看起来正是我想要的单个直方图: 好情节

我的第一个问题是,如果我添加ybar stacked到我的axis,我会收到一个错误:

! Package pgf Error: fill between: the mandatory argument 'of=<name path A> and
 <name path B> is missing or has empty arguments. Please ensure that the option
 has been set and that both path names have been assigned (perhaps you need 'na
me path global=line4' somewhere?).

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.56            \end{axis}

由于某种原因,我的垂直线没有绘制出来,导致这个错误。我必须移除它们才能移动一条(我怎样才能保留它们?)。

第二个问题是,即使删除了垂直分割(我需要保留),我也无法将直方图堆叠在一起:

\documentclass[11pt]{article}                                                    
\usepackage[top=2.54cm,bottom=2.54cm,left=2.75cm,right=2.75cm]{geometry}         
\usepackage{tikz}                                                                
\usetikzlibrary{calc}                                                            
\usepackage{pgfplots}                                                            
\usepgfplotslibrary{fillbetween}                                                 
\pgfplotsset{compat=1.17}                                                        
                                                                                 
\begin{filecontents}{data.txt}                                                   
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{filecontents}{data2.txt}                                                  
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{document}                                                                 
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} [                                                           
                ybar stacked,                                                    
                height = \axisdefaultheight,                                     
                width = \textwidth,                                              
                xlabel = Channel,                                                
                ylabel = Counts,                                                 
                ymin = 0,                                                        
                xmin = -5,                                                       
                xmax = 260,                                                      
                xtick pos = left,                                                
                axis on top,                                                     
            ]                                                                    
            % Vertical division of each Board [0-7]                              
            \addplot[                                                            
                ybar interval,                                                   
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black!20] table {data.txt};                               
            \addplot[                                                            
                ybar interval,                                                   
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black] table {data2.txt};                                 
        \end{axis}                                                               
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\end{document}  

由于某种原因,y堆叠条形图的值似乎是正确的,但条形图的填充不正确。我该如何在我的图中解决这两个问题?

坏的

答案1

最重要的一点是,正如 muzimuzhi Z 所建议的,使用ybar interval stacked。但是,使用图并不那么容易fill between,至少看起来并不简单。但是,你可以使用axis background键绘制阴影区域。

\documentclass[11pt]{article}                                                    
\usepackage[top=2.54cm,bottom=2.54cm,left=2.75cm,right=2.75cm]{geometry}         
\usepackage{pgfplots}                                                            
\usetikzlibrary{calc}                                                            
\usepgfplotslibrary{fillbetween}                                                 
\pgfplotsset{compat=1.17}                                                        
                                                                                 
\begin{filecontents}[overwrite]{data.txt}                                                   
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{filecontents}[overwrite]{data2.txt}                                                  
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{document}                                                                 
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} [                                                           
                ybar interval stacked,                                                    
                height = \axisdefaultheight,                                     
                width = \textwidth,                                              
                xlabel = Channel,                                                
                ylabel = Counts,                                                 
                ymin = 0,                                                        
                xmin = -5,                                                       
                xmax = 260,                                                      
                xtick pos = left,                                                
                axis on top,  
                axis background/.style={%https://tex.stackexchange.com/a/413306
                preaction={
                    path picture={
                        \path  (axis cs:0,0) coordinate (O) 
                         (path picture bounding box.north) coordinate (T)
                         foreach \x in {0,...,4} {(axis cs:64*\x-0.5,0) coordinate (x\x)}
                         (current axis.south east) coordinate (x5);                   
                        \foreach \x in {0,...,4}
                        {\ifodd\x\relax
                         \else
                         \fill[black!6] (x\x|-O) rectangle (x\the\numexpr\x+1\relax|-T);
                        \fi
                        \draw[black!6] (x\x|-O) -- (x\x|-T);}
                        }}}                                                   
            ]                                                                    
            \addplot[                                                            
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black!20] table {data.txt};                               
            \addplot[                                                            
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black] table {data2.txt};                                 
        \end{axis} 
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\end{document}  

在此处输入图片描述

相关内容