\pgfmathparse 中的计数器无法正常工作

\pgfmathparse 中的计数器无法正常工作

我有这个代码:

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

产生这样的结果:

在此处输入图片描述

这里的问题是 x 坐标的值。如上图所示,在 P1 中我有 10 100 10000,在 P2 中我有 10 1000 10000,在 P3 中我有 100 1000 16384。我想在这些值上添加一个固定数字,即 P1、P2、P3 中的 10 100 1000。我理解问题出在这段代码的某个地方:

 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}

也许在这里:\pgfmathparse{pow(10,int((1+\number\value{cheat})/2))}

但我不知道如何解决这个问题。有人知道吗?

答案1

以下是半自动化解决方案,需要手动设置类别数量(这里是 P1、P2、P3,所以是三个类别)。

它使用带有条形标签的数组(来源:在 PGFplots 中将月份名称缩写为刻度标签) 可通过计数器访问。每次绘制坐标cheat时,计数器都会增加。A bar

数据A bar是并行处理的,因此P1坐标对应于计数器值 0、1、2,坐标P2对应于 3、4、5,坐标对应于 6、7、8。因此,如果您除以类别数(此处为 3)并将结果取底,您将分别获得P3X 坐标的正确数组索引号 0、1、2 P1P2P3

梅威瑟:

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

结果:

在此处输入图片描述

相关内容