2

I have this code:

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

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

\pgfplotsset{compat=1.15}
\usetikzlibrary{patterns}
\newcounter{cheat}


\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}
\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  = 1.2*\textwidth,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=0.4cm,
        ymajorgrids = true,
        ylabel = {Response Time (sec)},
        symbolic x coords={P1,P2,P3},
        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={
                    anchor=north,xshift=0.2cm,
                    shift={(axis direction cs:P1,-\rawy)}
                }       
       ]
        \addplot[A bar]
            coordinates {(P1, 104.09) (P2,685.37) (P3,685.37)};
        \addplot[B bar]
            coordinates {(P1, 103.43) (P2,364.37) (P3,685.37)};

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

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

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

that procuces this:

enter image description here

The problem here is the values of x coords. As it is illustrated in above image, in P1 I have 10 100 10000, in P2 10 1000 10000 and in P3 100 1000 16384. I want to add a fix number on those values i.e. 10 100 1000 in P1,P2,P3. I understand the problem is somewhere in this code:

 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}

maybe here: \pgfmathparse{pow(10,int((1+\number\value{cheat})/2))}

but I do not know hot to fix this. Does anyone has an idea about this?

e7lT2P
  • 181

1 Answers1

1

The following is a semi-automatic solution which requires to manually set the number of categories (here P1, P2, P3, so three categories).

It uses an array with the bar labels (source: Abbreviated month names as tick labels in PGFplots) which is accessed using the cheat counter. The counter is increased every time an A bar coordinate is plotted.

The A bar data is processed in parallel, so the P1 coordinates correspond to counter values 0,1,2, the P2 coordinates to 3,4,5 and the P3 coordinates to 6,7,8. Therefore if you divide by the number of categories (here 3) and floor the result you get the correct array index numbers 0,1,2 for X coordinate P1, P2, P3 respectively.

MWE:

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

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

\pgfplotsset{compat=1.15}
\usetikzlibrary{patterns}
\newcounter{cheat}

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

\begin{document}
\def\sublabels{{"10","100","10,000"}}
\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={%
    \pgfmathparse{\sublabels[floor(\number\value{cheat}/3)]}%
    \pgfmathresult%
    \stepcounter{cheat}%
    },
    style={trolleygrey,fill=trolleygrey,mark=none},
    postaction={pattern=crosshatch dots},
    },
    B bar/.style={darkgray,fill=darkgray,mark=none}
}  
    \begin{axis}[
        width  = 1.2*\textwidth,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=0.4cm,
        ymajorgrids = true,
        ylabel = {Response Time (sec)},
        symbolic x coords={P1,P2,P3},
        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={
                    anchor=north,xshift=0.2cm,
                    shift={(axis direction cs:P1,-\rawy)}
                }       
       ]
        \addplot[A bar]
            coordinates {(P1, 104.09) (P2,685.37) (P3,685.37)};
        \addplot[B bar]
            coordinates {(P1, 103.43) (P2,364.37) (P3,685.37)};

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

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

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

Result:

enter image description here

Marijn
  • 37,699