删除条形图中的空白

删除条形图中的空白

我想删除条形图中与“kkk”对应的条形之间的空白。请提供建议。代码如下

笔记:改变顺序\addplot不起作用,因为它会改变绘制数据的含义。

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{mathtools}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage{hyperref}
\usepackage{subfig}
\usepackage{caption} \usetikzlibrary{fit,matrix,positioning,decorations.pathreplacing,calc,shapes,arrows,shadows,patterns}
\begin{document}
\begin{figure*}
    \centering
    \captionsetup{justification = centering}        
        \begin{tikzpicture}[scale =1.5, transform shape]
        \begin{axis}[   
        major x tick style = transparent,
        ybar = 2*\pgflinewidth,
        bar width=10pt,
        ymajorgrids = true,     
        symbolic x coords={ii, jj, kk},
        xtick=data, 
        scaled y ticks = false,
        enlarge x limits=0.35,
        ymin=0,
        minor y tick num=5, 
        xlabel style={yshift=-.3cm},
        ylabel style={yshift=-.3cm},    
        tick label style={font=\footnotesize},
        legend style={at={(.5,-0.1)}, anchor=north, /tikz/every even column/.append style={column sep=.2cm}},
        legend columns = -1,
        ]
        \addplot[style = {fill=blue, mark=none, postaction={pattern=dots}}, error bars/.cd, y dir=both, y explicit, error bar style ={thick}]
        coordinates {
            (ii, 53.980662) +- (0.443823296138724, 0.443823296138724)    
            (jj, 23.483776) +- (0.166719612668669, 0.166719612668669)   
            (kk, 131.7355) +- (2.47470029927319, 2.47470029927319)};                        

        \addplot[style = {fill=yellow, mark=none, postaction={pattern=north east lines}}, error bars/.cd, y dir=both, y explicit, error bar style ={thick}]
        coordinates {           
            (ii, 48.049724) +- (0.345152943568923, 0.345152943568923)
            (jj, 20.135426) +- (0.181081482002146, 0.181081482002146)
            (kk, 101.703804) +- (1.46905846775562, 1.46905846775562)};

        \addplot[style = {fill=red, mark=none, postaction={pattern=horizontal lines}}, error bars/.cd, y dir=both, y explicit, error bar style ={thick}]
        coordinates {
            (ii, 99.82356) +- (2.50417124042843, 2.50417124042843)  
            (jj, 62.805482) +- (4.10531953309646, 4.10531953309646)};

        \addplot[style = {fill=green, mark=none, postaction={pattern=vertical lines}}, error bars/.cd, y dir=both, y explicit, error bar style ={thick}]
        coordinates {
            (ii, 92.977176) +- (1.19822138752128, 1.19822138752128) 
            (jj, 63.35466) +- (1.52682237792326, 1.52682237792326)
            (kk, 71.74764) +- (1.13940206252386, 1.13940206252386) 
        };                      
        \legend{xxx, yyy, zzz, lll}
        \coordinate (A) at (axis cs:ii, 40.961);
        \coordinate (O1) at (rel axis cs:0,0);
        \coordinate (O2) at (rel axis cs:1,0);
        \draw [sharp plot,dashed, red, thick] (A -| O1) -- (A -| O2);
        \end{axis}      
        \end{tikzpicture}   
\end{figure*}
\end{document}

在此处输入图片描述

答案1

您可以使用如何在 pgfplots 中创建不同大小的条形图组?。请注意,它要求您将每个数据系列拆分为多个\addplot,以便单个数据系列创建的所有条形图\addplot都位于具有相同条形图数量的组中。在这种情况下,您需要例如在一个中绘制 和 的条形图,xxx在第二ii个中绘制的条形图。jj\addplotxxxkk\addplot

请注意,虽然不一定要为四个不同的条形定义和使用样式,但它确实减少了代码的重复,因为四种样式中的三种使用了两次。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}

\pgfplotsset{compat=1.3}

\pgfplotsset{ % from https://tex.stackexchange.com/questions/43832
    % #1: index in the group(0,1,2,...)
    % #2: number of plots of that group
    bar group size/.style 2 args={
        /pgf/bar shift={%
                % total width = n*w + (n-1)*skip
                % -> subtract half for centering
                -0.5*(#2*\pgfplotbarwidth + (#2-1)*\pgfkeysvalueof{/pgfplots/bar group skip})  + 
                % the '0.5*w' is for centering
                (.5+#1)*\pgfplotbarwidth + #1*\pgfkeysvalueof{/pgfplots/bar group skip}},%
    },
    bar group skip/.initial=2pt,
    errbars/.style={error bars/.cd, y dir=both, y explicit, error bar style ={thick},/pgfplots/.cd},
    plot 0/.style={fill=blue, mark=none, postaction={pattern=dots},errbars},%
    plot 1/.style={fill=yellow, mark=none, postaction={pattern=north east lines},errbars},%
    plot 2/.style={fill=red, mark=none, postaction={pattern=horizontal lines},errbars},%
    plot 3/.style={fill=green, mark=none, postaction={pattern=vertical lines},errbars}
}


\begin{document}
        \begin{tikzpicture}[scale =1.5, transform shape]
        \begin{axis}[   
        major x tick style = transparent,
        ybar = 2*\pgflinewidth,
        bar width=10pt,
        ymajorgrids = true,     
        symbolic x coords={ii, jj, kk},
        xtick distance=1, % instead of xtick=data
        scaled y ticks = false,
        enlarge x limits=0.35,
        ymin=0,
        minor y tick num=5, 
        xlabel style={yshift=-.3cm},
        ylabel style={yshift=-.3cm},    
        tick label style={font=\footnotesize},
        legend style={at={(.5,-0.1)}, anchor=north, /tikz/every even column/.append style={column sep=.2cm}},
        legend columns = -1,
        ]
        \addplot[bar group size={0}{4}, plot 0]
        coordinates {
            (ii, 53.980662) +- (0.443823296138724, 0.443823296138724)    
            (jj, 23.483776) +- (0.166719612668669, 0.166719612668669)};

        \addplot [bar group size={0}{3}, plot 0, forget plot]
        coordinates {
            (kk, 131.7355) +- (2.47470029927319, 2.47470029927319)};

        \addplot[bar group size={1}{4}, plot 1]
        coordinates {           
            (ii, 48.049724) +- (0.345152943568923, 0.345152943568923)
            (jj, 20.135426) +- (0.181081482002146, 0.181081482002146)};

        \addplot [bar group size={1}{3}, plot 1, forget plot]
        coordinates {
            (kk, 101.703804) +- (1.46905846775562, 1.46905846775562)};

        \addplot[bar group size={2}{4},plot 2]
        coordinates {
            (ii, 99.82356) +- (2.50417124042843, 2.50417124042843)  
            (jj, 62.805482) +- (4.10531953309646, 4.10531953309646)};

        \addplot[bar group size={3}{4}, plot 3]
        coordinates {
            (ii, 92.977176) +- (1.19822138752128, 1.19822138752128) 
            (jj, 63.35466) +- (1.52682237792326, 1.52682237792326)};

        \addplot [bar group size={2}{3}, plot 3, forget plot]
        coordinates {
            (kk, 71.74764) +- (1.13940206252386, 1.13940206252386) 
        };                      
        \legend{xxx, yyy, zzz, lll}
        \coordinate (A) at (axis cs:ii, 40.961);
        \coordinate (O1) at (rel axis cs:0,0);
        \coordinate (O2) at (rel axis cs:1,0);
        \draw [sharp plot,dashed, red, thick] (A -| O1) -- (A -| O2);

        \end{axis}      
        \end{tikzpicture}   
\end{document}

相关内容