1

I am trying to plot a bar chart using tikz. I am struggling with placing numbers above the bars. I use the "nodes near coords" option but I always get "undefinded control sequence" error.

I know, there have been several threads about this but none of the solutions worked for me. I am using "symbolic x coords" in math mode for the x ticks. I assume this is the problem because if I remove math mode, no error is thrown. Here is my MWE:

\documentclass[border=0.2cm]{standalone}


% Bar chart drawing library 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
 
\begin{document}
 
\begin{tikzpicture}
\begin{axis}[
    ymajorgrids=true,
    grid style=dashed,
    ybar,
    x label style={at={(axis description cs:0.5,-0.2)},anchor=north},
    symbolic x coords={$\mathit{test}$,$\mathit{test}\_2$,$\mathit{test}\_3$},
    xtick=data,
    xticklabel style={rotate=-45, anchor=north west},
    xlabel={Number of children},
    ylabel={Number},
    xticklabel style = {font=\small},
    nodes near coords
    ]
    \addplot coordinates {
        ($\mathit{test}$,8506)
        ($\mathit{test}\_2$,1429)
        ($\mathit{test}\_3$,4)
    };
\end{axis}
\end{tikzpicture}
 
\end{document}

Overleaf actually compiles and outputs the right figure but with errors... It looks like this:

Output from Overleaf

I would be extremely grateful if someone could help me to get rid of the errors... It's for my thesis.

THANK YOU :D

EDIT: Apparently the \mathit{} causes the problems... but I need to use it -_-

2 Answers 2

1

I found a solution!!!!! :D

Instead of "symbolic x coords", I used the "xtick" and "xticklables" option! Works with \mathit

Here the code:

\documentclass[border=0.2cm]{standalone}
 
% Bar chart drawing library 
\usepackage{pgfplots}
\usepackage{amsmath}
\pgfplotsset{compat=newest}
 
\begin{document}
 
\begin{tikzpicture}
\begin{axis}[
    ymajorgrids=true,
    grid style=dashed,
    ybar,
    x label style={at={(axis description cs:0.5,-0.2)},anchor=north},
    xtick={1,3},xticklabels={$\mathit{test}$,$\mathit{test}\_2$, $\mathit{test}\_3$},
    xtick=data,
    xticklabel style={rotate=-45, anchor=north west},
    xlabel={Number of children},
    ylabel={Number},
    xticklabel style = {font=\small},
    nodes near coords
    ]
    \addplot coordinates {
        (1,8506)
        (2,1429)
        (3,4)
    };
\end{axis}
\end{tikzpicture}
\end{document}

I hope this answer is useful for someone in the future haha ;)

0

Hm, don't know, where you get the data for the x-coordinates from, but at least, this is the form you need to follow:

    \documentclass[border=0.2cm]{standalone}
    
    
    % Bar chart drawing library 
    \usepackage{pgfplots}
    %\pgfplotsset{compat=newest}
     
    \begin{document}
     
    \begin{tikzpicture}
    \begin{axis}
    \addplot 
%   [ybar]    % remove comment to draw as ybar-chart
    coordinates {
            (0,8506)
            (1,1429)
            (2,4)
        };
    \end{axis}
    \end{tikzpicture}
     
    \end{document}

Suggestion: gradually add your extras and do a control run every now and then. Taking mini-steps help you failing early at identifyable places ;-)

Results:

Example Bar plot example

P.S.: see also the pgfplots-manual, e.g. here:

Chapter on bar plots

2
  • Thank you very much for your answer!!! :) I actually tried ministeps and I get the error when I use $$ (mathmode): symbolic x coords={$\mathit{test}$,$\mathit{test}\_2$,$\mathit{test}\_3$}. (This is also the data for my x-coordinates). Without mathmode it works fine.
    – swageta
    Commented Sep 13, 2021 at 14:52
  • 1
    Great :) Glad I could help you a little bit.
    – MS-SPO
    Commented Sep 13, 2021 at 16:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .