pgfplots 条形图持续出现错误

pgfplots 条形图持续出现错误

我一直在尝试找出我的代码出了什么问题,我正尝试使用下面的数据 (+ 代码) 制作条形图。一些输出包括:

  • \output 处于活动状态时,发生 \vbox 过满(10.22557pt 太高)
  • 包 pgfplots 错误:抱歉,输入坐标“OTPr”尚未定义为“符号 x 坐标 = {11-331p,11-332p,11-333p,11-334p,11-335p,11-336 p,11-337p,11-338p,11-339p,11-340p,11-341p,11-342p,11-343p,11-344p,11-345p,11-346p,11-347p,11-348p,11-48p,11-49p,11-50p,11-51p}...也许是拼写错误?或者您的意思是像 [normalized]OTPr? 这样的东西。
\pgfplotstableread[row sep=\\,col sep=&]{
      Endorsements & Zero & One \\
      OTPr & 1.29 & 4.20 \\ 
      OTPa & 2.80 & 3.16 \\ 
      ASTPr & 3.24 & 2.37 \\ 
      ASTPa & 4.35 & 1.18 \\ 
      OTCPr & 5.79 & 0.14 \\ 
      OTCPa & 6.00 & 1.18 \\ 
      IFOPr & 7.21 & 2.45 \\ 
      IFOPa & 8.38 & 3.28 \\ 
      CSOPr & 9.06 & 4.66 \\ 
      CSOPa & 8.58 & 5.35 \\ 
      CTPr & 7.99 & 6.24 \\ 
      CTPa & 6.97 & 7.24 \\ 
      CADEPr & 5.38 & 8.49 \\
      CADEPa & 4.40 & 9.64 \\ 
      IFCPr & 3.92 & 8.40  \\ 
      IFCPa & 2.26 & 7.26 \\ 
      CSCPr & 1.18 & 6.55 \\ 
      CSCPa & 2.67 & 5.30 \\ 
      OPr & 3.82 & 4.12 \\ 
      OPa & 4.53 & 3.88 \\ 
      CPr & 5.38 & 2.49 \\ 
      CPa & 6.45 & 1.64 \\
    }\mydataC
    
    \begin{figure}[H]
    \begin{tikzpicture}
    \begin{axis}[
      enlarge x limits={rel=0.5},
      ybar = 3,
      bar width=0.75cm,
      width=\textwidth,
      height=.4\textwidth,
      legend style={at={(0.5,1)},
      anchor=north,legend columns=-1},
      symbolic x coords={11-331p, 11-332p, 11-333p, 11-334p, 11-335p, 11-336p, 11-337p, 11-338p, 11-339p, 11-340p, 11-341p, 11-342p, 11-343p, 11-344p, 11-345p, 11-346p, 11-347p, 11-348p, 11-48p, 11-49p, 11-50p, 11-51p},
      xtick=data,
      nodes near coords={
        \pgfmathfloattofixed{\pgfplotspointmeta} 
        \num[
          round-mode = places,
          round-precision = 2,
        ]{\pgfmathresult}},
      nodes near coords align={vertical},
      ymin=0,ymax=10,
      ylabel={Rates (\%)},
    ]
    \addplot table[x=Endorsements,y=Zero]{\mydataC};
    \addplot table[x=Endorsements,y=One]{\mydataC};
    \legend{$Zero$, $One$},
    \end{axis}
    \end{tikzpicture}
    \caption{Bar chart.}
    \end{figure}

答案1

欢迎来到 TeX.SE!

您的表格symbolic x coords中未定义\mydataC(如错误消息所示)。因此,不清楚您希望通过定义symbolic x coords.X 坐标标签实现什么目的?如果是这样,那么这是错误的方法。

对我来说,这是合乎逻辑的,你遵循以下做法:

示例 1

制作方:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotstableread[row sep=\\,col sep=&]{
     Endorsements & Zero & One \\
      OTPr      & 1.29 & 4.20 \\
      OTPa      & 2.80 & 3.16 \\
      ASTPr     & 3.24 & 2.37 \\
      ASTPa     & 4.35 & 1.18 \\
      OTCPr     & 5.79 & 0.14 \\
      OTCPa     & 6.00 & 1.18 \\
      IFOPr     & 7.21 & 2.45 \\
      IFOPa     & 8.38 & 3.28 \\
      CSOPr     & 9.06 & 4.66 \\
      CSOPa     & 8.58 & 5.35 \\
      CTPr      & 7.99 & 6.24 \\
      CTPa      & 6.97 & 7.24 \\
      CADEPr    & 5.38 & 8.49 \\
      CADEPa    & 4.40 & 9.64 \\
      IFCPr     & 3.92 & 8.40 \\
      IFCPa     & 2.26 & 7.26 \\
      CSCPr     & 1.18 & 6.55 \\
      CSCPa     & 2.67 & 5.30 \\
      OPr       & 3.82 & 4.12 \\
      OPa       & 4.53 & 3.88 \\
      CPr       & 5.38 & 2.49 \\
      CPa       & 6.45 & 1.64 \\
    }\mydataC
    
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{siunitx}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[x=10mm,
    ybar=1mm,
    bar width=3.4mm,
    enlarge x limits=0.05,
    enlarge y limits={.15, upper},
% y ticks style and label
    ymin=0, ymax=10,
    ylabel={Rates (\si{\percent})},
%
    nodes near coords,
    nodes near coords style={rotate=90, anchor=west},
% x axis ticks and style
    xtick=data,
    xticklabels from table={\mydataC}{Endorsements},    % <-------------------
    table/x expr = \coordindex,                         % <-------------------
    x tick label style = {rotate=45, anchor=east, inner sep=2pt},
% legend
    legend style = {legend pos=north east,
                    legend columns=-1,
                    /tikz/every even column/.append style={column sep=1em}
                    },
    ]
\addplot [fill=blue!80!black]
            table [y=Zero]  {\mydataC};
            \addlegendentry{Zero};
\addplot [fill=yellow!80!black]
            table [y=One]  {\mydataC};
            \addlegendentry{One};
    \end{axis}
\end{tikzpicture}
\end{document}

如果您想更改xtick标签,则需要替换代码行

xticklabels from table={\mydataC}{Endorsements},

    xticklabels={11-331p, 11-332p, 11-333p, 11-334p, 11-335p, 11-336p, 11-337p, 11-338p, 11-339p, 11-340p, 11-341p, 11-342p, 11-343p, 11-344p, 11-345p, 11-346p, 11-347p, 11-348p, 11-48p, 11-49p, 11-50p, 11-51p},

你将获得:

示例 2

相关内容