2

I am trying to add labels on top of the bars in my graph but it is not working. I want it to be the y-value that shows up but instead its numbering the bars.

This is my code so far:

\documentclass[12pt]{article}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\usepackage{eurosym}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{float}
\usepackage{caption}
\usepackage{calc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lastpage}
\usepackage{pdflscape}
\usepackage{times}
\captionsetup{justification=raggedright,singlelinecheck=false}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}

\tikzstyle{chart}=[
legend label/.style={font={\Large},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=10pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}}]

\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
\begin{minipage}{0.96\linewidth}
\flushleft
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar,ybar legend,fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
    xbar,
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    symbolic x coords={Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton},
    xtick=data,
    nodes near coords,
    yticklabel style={/pgf/number format/fixed},,
    ytick pos=left,
    axis y line*=left,
    xtick pos=bottom,
    axis x line*=bottom,
    legend style={draw=none,at={(0,1.03)},anchor=south west},
    legend columns=-1,
    xtick align=center,
    ytick align=center,
    xtick distance=0.2,
    ytick distance=,
    x tick label style ={font=\normalsize,text width=0.7cm,anchor=north,rotate=0,align=center},
    y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
    scaled y ticks=false,
    bar width=45pt,
    ymajorgrids,
    xlabel=\textbf{Teams},
    ylabel=\textbf{Points},
    title=\textbf{Team Points},
    ,
    ]
        \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
        x;  y;  z
        Total;  32; 0
        Curie;  4;  0
        Franklin;   6;  0
        Angela; 2;  0
        Ursula; 1;  0
        Warner; 5;  0
        Newton; 8;  0
        Hyems;  5;  0
        Repton; 1;  0
        };
        %Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}

and the current output: values ontop of the bar do not match y-coord

3
  • 1
    Hi and welcome. Please delete the packages that are not useful in the problem. That is, give a minimal example as explained here: How to make a “minimum example”
    – AndréC
    May 28, 2021 at 11:48
  • ok, i have done that
    – texstar
    May 28, 2021 at 11:53
  • 1
    Did AndreC's answer solve your problem or do you need further assistance? If yes, what is the (remaining) problem? Jun 28, 2021 at 19:35

1 Answer 1

1

I commented the code to show the change. Just delete the xbar line. I have deleted the useless packages for this problem.

enter image description here

\documentclass[12pt]{article}
\usepackage{fancyhdr}
\renewcommand{\familydefault}{\sfdefault}
\renewcommand*{\ttdefault}{\familydefault}
\usepackage[paperwidth=42cm,paperheight=29.7cm,left =1cm, top = 1cm, right =1cm, bottom = 1cm ,marginparwidth=0cm, includeheadfoot,headheight=66pt, headsep=0cm]{geometry}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=newest}
\usetikzlibrary{plotmarks}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{units}
\tikzset{every picture/.append style={font=\normalsize}} % size graph font
\usetikzlibrary{arrows, positioning, calc}

\tikzstyle{chart}=[
legend label/.style={font={\Large},anchor=west,align=left},
legend box/.style={rectangle, draw, minimum size=10pt},
axis/.style={black,semithick,->},
axis label/.style={anchor=east,font={\tiny}}]

\definecolor{customcolor}{HTML}{1d5893}
\begin{document}
\begin{minipage}{0.96\linewidth}
\flushleft
\begin{tikzpicture}
\pgfplotscreateplotcyclelist{defaultCycle}{%
ybar,%ybar legend,
fill=customcolor,draw=black,opacity=1,thin,solid,mark=no,mark options=solid,\\%
}
\begin{axis}
[
%    xbar, <--- comment this line 
    cycle list name=defaultCycle,
    width=0.96\linewidth,
    height=0.15\textheight,
    use units,
    scale only axis,
    symbolic x coords={Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton},
    xtick=data,
    nodes near coords,
    yticklabel style={/pgf/number format/fixed},,
    ytick pos=left,
    axis y line*=left,
    xtick pos=bottom,
    axis x line*=bottom,
    legend style={draw=none,at={(0,1.03)},anchor=south west},
    legend columns=-1,
    xtick align=center,
    ytick align=center,
    xtick distance=0.2,
    ytick distance=,
    x tick label style ={font=\normalsize,text width=0.7cm,anchor=north,rotate=0,align=center},
    y tick label style ={font=\normalsize,text width=2cm,anchor=east,rotate=0,align=right},
    scaled y ticks=false,
    bar width=45pt,
    ymajorgrids,
    xlabel=\textbf{Teams},
    ylabel=\textbf{Points},
    title=\textbf{Team Points},
    ,
    ]
        \addplot+ table [x={x},y={y},meta index=2,col sep=semicolon] {
        x;  y;  z
        Total;  32; 0
        Curie;  4;  0
        Franklin;   6;  0
        Angela; 2;  0
        Ursula; 1;  0
        Warner; 5;  0
        Newton; 8;  0
        Hyems;  5;  0
        Repton; 1;  0
        };
        %Total,Curie,Franklin,Angela,Ursula,Warner,Newton,Hyems,Repton
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}
2
  • thank you, i have also tried this code with some negative values and it is overlapping with the axis. is there a way i can always have the values at the top of the bar.
    – texstar
    May 28, 2021 at 12:11
  • By default, it is the key nodes near coords that displays the values of y next to it. In the case of vertical bars, above the bars.
    – AndréC
    May 28, 2021 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.