0

I'm drawing a bar chart with pgfplots. I need to have the value of each column above the column itself, like this:

Figure 1

center aligned bar chart labels

but LaTeX centers it above the left side:

Figure 2

left aligned bar chart labels

I tried using nodes near coords align={vertical} and nodes near coords align={horizontal} as axis environment's options but the first doesn't do anything and the second makes it worse:

Figure 3

enter image description here

How can I put the labels where I want them without overlapping each other nor the near columns (see value 4000 in the 90 column in figure 2)? I would also like to hide the label for the "hidden" 100 column (visible in figures 2 and 3).


Here's a simplified version of the source (possibly overindented, but I find it clearer):

\documentclass[twocolumn]{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[xtick=data,
                 xtick pos=left,
                 xticklabel interval boundaries,
                 ybar,
                 ymin=0,
                 scaled y ticks=false,
                 nodes near coords,
                 symbolic x coords={$0$,$10$,$20$,$30$,$40$,$50$,$60$,$70$,$80$,$90$,$100$}]
        \addplot [ybar interval,fill=lightgray]
                 coordinates {($0$, 5)     ($10$, 0)    ($20$, 15)
                              ($30$, 75)   ($40$, 250)  ($50$, 1000)
                              ($60$, 3000) ($70$, 7000) ($80$, 11000)
                              ($90$, 4000) ($100$, 0)};
        \end{axis}
    \end{tikzpicture}
\end{document}

N.B.: the class option twocolumn seems to be relevant for the label alignment.

1 Answer 1

1

Try the following:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{center}
    \begin{tikzpicture}
\begin{axis}[
    width=\linewidth,
    xtick=data,
    xticklabel interval boundaries,
    ybar,
    %bar width=9mm,
    ymin=0,
    scaled y ticks=false,
    nodes near coords,
    nodes near coords style = {font=\footnotesize, anchor=south west, 
                               text width=8mm, inner xsep=0pt, align=center},
    symbolic x coords={$0$,$10$,$20$,$30$,$40$,$50$,$60$,$70$,$80$,$90$,$100$}
            ]
\addplot [ybar interval,fill=lightgray]
         coordinates {($0$, 5)     ($10$, 0)    ($20$, 15)
                      ($30$, 75)   ($40$, 250)  ($50$, 1000)
                      ($60$, 3000) ($70$, 7000) ($80$, 11000)
                      ($90$, 4000) ($100$, 0)};
\end{axis}
    \end{tikzpicture}
\end{center}
\end{document}

enter image description here

Addendum:

According to your comment, that width of the diagram is equal to columnwidth and that bars' labels can be rotated:

\documentclass[twocolumn]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{lipsum}

\begin{document}
\lipsum[11]
\begin{figure}[ht]
    \begin{tikzpicture}
\begin{axis}[
    width=\linewidth,
    ybar,
    xtick=data,
    xticklabel interval boundaries,
    ytick distance=2000,
    xticklabel style= {font=\footnotesize},
    xmax=110, ymin = 0, % had to be smaller than y coordinate
                        % of dummy data y coordinate
    enlarge x limits=0.05,
    enlarge y limits={value=.3,upper},
    scaled y ticks=false,
    nodes near coords,
    nodes near coords style = {font=\small, 
                               xshift=0.75*\pgfkeysvalueof{/pgf/bar width},
                               rotate=90, anchor=west},
    symbolic x coords={0,10,20,30,40,50,60,70,80,90,100,110}
            ]
\addplot [ybar interval,fill=lightgray]
     coordinates {(0,     5) (10,   0) (20,  15) (30,  75)   
                  (40,  250) (50,1000) (60,3000) (70,7000) 
                  (80,11000) (90,4000) (100,  0) (100, 0)
                  (110,-100) % dummy coordinate,
                 };          % option clip=false is prohibited
\end{axis}
    \end{tikzpicture}
\caption{Diagram}
\label{fig:diagram}
    \end{figure}

See diagram on figure {fig:diagram}
\lipsum[1-4]
\end{document}

enter image description here

8
  • I don't know much pgfplots nor tikz, but I think I understand how it should work. However, it doesn't for me: it aligns too much to the right, the most digits, the worst. What I get is this.
    – Ntakwetet
    May 16, 2020 at 7:01
  • @Ntakwetet, not with my MWE. You change something in it.
    – Zarko
    May 16, 2020 at 7:19
  • Ouch! Sorry, you're reason - it works when I compile it alone, but it breaks in my document. I managed to reproduce the problem, it happens when the document is in twocolumn. Is it due to the width=\linewidth? How can I fix that?
    – Ntakwetet
    May 16, 2020 at 7:47
  • @Ntakwetet, first you need to edit your question and provide relevant information about your document. Over thumb, try to reduce font size. \linewidth works in two column document too, however, in it is diagram narrower (with with of a column). See, if it is acceptable, that image span two columns?
    – Zarko
    May 16, 2020 at 8:00
  • I just updated it with the MWE with the twocolumn class option. Unluckily I don't know what informations are relevant until I don't try a solution: with my original code twocolumn wasn't relevant - the chart was broken as well, so I just pruned it =). I reduced the font size to \tiny, but the problem is still there and the labels are barely readable. If nothing else works I'll span the columns, but I would rather prefere not to do so - the result isn't very good-looking (but I can work on that).
    – Ntakwetet
    May 18, 2020 at 21:45

You must log in to answer this question.

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