1

I have this tikzpicture code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{tikz}


\definecolor{trolleygrey}{rgb}{0.5, 0.5, 0.5}
\definecolor{darkgray}{rgb}{0.66, 0.66, 0.66}

\title{pgfplots}
\author{antonis.makris2411 Mak}
\date{July 2019}



\begin{document}

\maketitle


\begin{figure*}[!htbp]
  \begin{tikzpicture}[scale=0.45]
    \begin{axis}[
        width  = 1.15*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=0.4cm,
        ymajorgrids = true,
        ylabel = {Response Time (sec)},
        symbolic x coords={P1,P2},
        xtick = data,
        scaled y ticks = false,
        enlarge x limits=0.25,
        x tick label style={font=\Large},
        y tick label style={font=\Large},
        y label style={font=\Large},
        ymin=0,
        legend cell align=left,
        legend style={font=\Large,draw=none, legend columns=-1}
       ]

       \addplot[style={trolleygrey,fill=trolleygrey,mark=none},postaction={pattern=crosshatch dots}]
            coordinates {(P1, 104.09) (P2,685.37)};
         \addplot[style={darkgray,fill=darkgray,mark=none}]
            coordinates {(P1, 103.43) (P2,364.37)};

        \addplot[style={trolleygrey,fill=trolleygrey,mark=none},postaction={pattern=crosshatch dots}]
              coordinates {(P1,495.74) (P2,454.20)};
              \addplot[style={darkgray,fill=darkgray,mark=none}]
            coordinates {(P1, 364.43) (P2,364.37)};

          \addplot[style={trolleygrey,fill=trolleygrey,mark=none},postaction={pattern=crosshatch dots}]
           coordinates {(P1,1929.74) (P2,454.20)};
         \addplot[style={darkgray,fill=darkgray,mark=none}]
              coordinates {(P1,699.54) (P2,454.20)};

        \legend{S1, S2}
    \end{axis}
  \end{tikzpicture}% pic 1
\end{figure*}



\end{document}

which produces this:

enter image description here

Is it possible to add an extra info for x axis (10,100,1000) and some space between the bars? To explain better what I am trying to do with a small example:

enter image description here

  • Can't you just add empty plots where you want to have the gaps? And please make your code compilable, i.e. start with \documentclass and end with \end{document} and load all the packages. Also it is not recommended to pass options like scale=0.45 to the ambient tikzpicture. If at all, pass them to the axis environment, or just choose the width and height of it appropriately. – user121799 Jul 14 '19 at 11:39
  • Hello @marmot. I edited my question. As you can see I want to create an extra x coords for bars. every two bars I have a x coords value and an extra space – Telperinquar Jul 14 '19 at 14:10
1

The bar shift is computed according to a formula that can be found on p. 83 of pgplotsmanual v 1.16. This formula can be modified, which has been done e.g. here. So I replaced (essentially) \numplotsofactualtype by \numplotsofactualtype+int(\numplotsofactualtype/2) which adds an offset after every other plot. The extra numbers can be inserted as nodes near coords using this answer.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{patterns}

\definecolor{trolleygrey}{rgb}{0.5, 0.5, 0.5}
\definecolor{darkgray}{rgb}{0.66, 0.66, 0.66}

\title{pgfplots}
\author{antonis.makris2411 Mak}
\date{July 2019}
\newcounter{cheat}


\begin{document}

\maketitle


\begin{figure*}[!htbp]
  \begin{tikzpicture}
\pgfplotsset{
    /pgfplots/bar shift auto/.style={
        /pgf/bar shift={
        -0.5*(int(3/2*\numplotsofactualtype-1)*\pgfplotbarwidth 
        + (int(3/2*\numplotsofactualtype-1))*(#1))  +
          (.5+\plotnumofactualtype+int(\plotnumofactualtype/2))*\pgfplotbarwidth 
          + \plotnumofactualtype*(#1)
        },
    },
    A bar/.style={nodes near coords={\stepcounter{cheat}%
    \pgfmathparse{pow(10,int((1+\number\value{cheat})/2))}%
    \pgfmathprintnumber\pgfmathresult},
      style={trolleygrey,fill=trolleygrey,mark=none},postaction={pattern=crosshatch
    dots}},
    B bar/.style={darkgray,fill=darkgray,mark=none}
}  
    \begin{axis}[
        width  = 0.9*\textwidth,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=0.4cm,
        ymajorgrids = true,
        ylabel = {Response Time (sec)},
        symbolic x coords={P1,P2},
        xtick = data,
        scaled y ticks = false,
        enlarge x limits=0.35,
        x tick label style={font=\Large,yshift=-10pt},
        y tick label style={font=\Large},
        y label style={font=\Large},
        ymin=0,
        legend cell align=left,
        legend style={font=\Large,draw=none, legend columns=-1},
        visualization depends on=y \as \rawy,
        every node near coord/.append style={% https://tex.stackexchange.com/a/110887/121799
                    anchor=north,xshift=0.2cm,
                    shift={(axis direction cs:P1,-\rawy)}
                }       
       ]

        \addplot[A bar]
            coordinates {(P1, 104.09) (P2,685.37)};
        \addplot[B bar]
            coordinates {(P1, 103.43) (P2,364.37)};

        \addplot[A bar]
              coordinates {(P1,495.74) (P2,454.20)};
        \addplot[B bar]
            coordinates {(P1, 364.43) (P2,364.37)};

        \addplot[A bar]
           coordinates {(P1,1929.74) (P2,454.20)};
        \addplot[B bar]
              coordinates {(P1,699.54) (P2,454.20)};

        \legend{S1, S2}
    \end{axis}
  \end{tikzpicture}% pic 1
\end{figure*}
\end{document}

enter image description here

  • Hello @marmot. This is great. Thank you. Do you know how to solve the second part? I mean how to add extra x axis values like image in my question. I want to add xcoord 10 under the first two bars, 100 under the middle two and 1000 under the last bars. – Telperinquar Jul 15 '19 at 6:39
  • @Telperinquar I added them. – user121799 Jul 15 '19 at 7:54
  • Thank you very much! It works! One last question @marmot. Is it possible to change thw font in 10,100,1000? In order to be more visible? – Telperinquar Jul 15 '19 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.