2

I want to remove the white space between the bars corresponding to 'kkk' in the bar graph. Please advice. Code is give below

Note: Changing order of \addplot doesn't work, as it changes the meaning of plotted data.

\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}

enter image description here

Torbjørn T.
  • 206,688
  • I hope you don't mind that I added an image of the output of your MWE, I think it helps understanding your question better. – daleif Jul 03 '18 at 06:58
  • 2
    That's not whitespace but rather shows zzz variable is zero. As you mentioned the order matters and that's why you get an empty spot there to show it. – percusse Jul 03 '18 at 07:27
  • @percusse I know it shows zzz equal to 0. I wants to know if there is any method to remove the entry of zzz from kk. – Yogesh Sharma Jul 03 '18 at 07:52
  • See if https://tex.stackexchange.com/questions/43832/how-can-i-create-bar-plot-groups-of-different-sizes-in-pgfplots helps. – Torbjørn T. Jul 03 '18 at 07:53
  • @TorbjørnT. Thanks for the link but it is not working because my plots have different parameters than given in the link. – Yogesh Sharma Jul 03 '18 at 09:09
  • It does work, so you must have done something wrong. – Torbjørn T. Jul 03 '18 at 10:07

1 Answers1

3

You can use the method described in How can i create bar plot groups of different sizes in pgfplots?. Note it requires you to split each data series in multiple \addplots, so that all the bars created by a single \addplot are in groups that has the same number of bars. In this case, you need for example to plot the bars for xxx at ii and jj in one \addplot and the bar for xxx at kk in a second \addplot.

Note that while it's not strictly necessary to define and use styles for the four different bars, it does make for less repetition of code, as three of the four styles are used twice.

enter image description here

\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}
Torbjørn T.
  • 206,688