TransWikia.com

Persistent errors with pgfplots bar graphs

TeX - LaTeX Asked by FranjoIM on August 2, 2021

I’ve been trying to figure out what’s wrong with my code, I’m trying to make a bar chart with the data below (+ code). Some of the outputs include:

  • Overfull vbox (10.22557pt too high) has occurred while output is active
  • Package pgfplots Error: Sorry, the input coordinate `OTPr’ has not been defin
    ed with ‘symbolic x coords={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-3
    45p, 11-346p, 11-347p, 11-348p, 11-48p, 11-49p, 11-50p, 11-51p}… Maybe it has
    been misspelled? Or did you mean something like [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=.4textwidth,
      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}

One Answer

Welcome to TeX.SE!

Your symbolic x coords are not defined in mydataC table (as notified you error's message) . So, it is not clear, what you like to achieve by defining your symbolic x coords. X-coordinates labels? If so, this is wrong approach.

To me is somehow logical, that you after the following:

example 1

which is produced by:

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}

IN the case, that you like to change xtick labels, than you need to replace code line

xticklabels from table={mydataC}{Endorsements},

with

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

and you will get:

example 2

Correct answer by Zarko on August 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP