1

I have bar chart where my node labels are overflowing. What is the best way to solve this?

enter image description here

\documentclass{article}
\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
  \begin{axis}[
    title=sample count per class,
    xbar, xmin=0,
    width=8cm, height=6cm, enlarge y limits=0.15,
    symbolic y coords={{,},{.},{-},{:}, {?}},
    ytick=data,
    nodes near coords, nodes near coords align={horizontal},
    point meta=rawx,
    nodes near coords style={/pgf/number format/.cd,fixed},
    ]
    \addplot [xbar] coordinates {
    (1290282,{.})
    (2208970,{,})
    (81710,{-})
    (51192,{:})
    (40511,{?})
    };
  \end{axis}
\end{tikzpicture}

\end{document}

2 Answers 2

2

pgfplots xmax

\documentclass{article}
\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
  \begin{axis}[
    title=sample count per class,
    xbar, xmin=0,xmax=3000000,
    width=8cm, height=6cm, enlarge y limits=0.15,
    symbolic y coords={{,},{.},{-},{:}, {?}},
    ytick=data,
    nodes near coords, nodes near coords align={horizontal},
    point meta=rawx,
    nodes near coords style={/pgf/number format/.cd,fixed},
    ]
    \addplot [xbar] coordinates {
    (1290282,{.})
    (2208970,{,})
    (81710,{-})
    (51192,{:})
    (40511,{?})
    };
  \end{axis}
\end{tikzpicture}

\end{document}
2
  • 1
    That's a neat little hack and it looks great. Thank you @SebGlav
    – Oliver
    May 13, 2021 at 14:24
  • 1
    Sometimes a little hack is worth a complex solution. But other things are possible (like writing vertically instead of horizontally).
    – SebGlav
    May 13, 2021 at 15:38
1

Here I present an alternative, if you don't want to enlarge the ymax. For details, please have a look at the comments in the code.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
% we need PGFPlotsTable ...
\usepackage{pgfplotstable}
    % ... to store the data in an inline table
    % (alternatively one could store it in a file)
    \pgfplotstableread{
        x       y
        1290282 {.}
        2208970 {,}
        81710   {-}
        51192   {:}
        40511   {?}
    }{\MyData}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        title=sample count per class,
        xbar, xmin=0,
        width=8cm, height=6cm, enlarge y limits=0.15,
%        % this avoids the need for `symbolic y coords` ...
%        symbolic y coords={{,},{.},{-},{:},{?}},
        % ... and instead we can load them from the table
        yticklabels from table={\MyData}{y},
        ytick=data,
        % ---------------------------------------------------------------------
        % adjusted from <https://tex.stackexchange.com/a/335742/95441>
        % ---------------------------------------------------------------------
        % #1: the THRESHOLD after which we switch to a special display.
        nodes near coords custom/.style={
            small value/.style={
                % everything is fine here, so I think there is no need
                % to change something
            },
            large value/.style={
                color=white,
                anchor=east,
            },
            every node near coord/.style={
              /pgf/number format/fixed,
              check for zero/.code={%
                \pgfmathfloatifflags{\pgfplotspointmeta}{0}{%
                    % If meta=0, make the node a coordinate (which doesn't have text)
                    \pgfkeys{/tikz/coordinate}%
                }{%
                    \begingroup
                    % this group is merely to switch to FPU locally. Might be
                    % unnecessary, but who knows.
                    \pgfkeys{/pgf/fpu}%
                    \pgfmathparse{\pgfplotspointmeta<#1}%
                    \global\let\result=\pgfmathresult
                    \endgroup
                    %
                    % simplifies debugging:
                    %\show\result
                    %
                    \pgfmathfloatcreate{1}{1.0}{0}%
                    \let\ONE=\pgfmathresult
                    \ifx\result\ONE
                        % AH : our condition 'y < #1' is met.
                        \pgfkeysalso{/pgfplots/small value}%
                    \else
                        % ok, proceed as usual.
                        \pgfkeysalso{/pgfplots/large value}%
                    \fi
                }
              },
              check for zero,
            },
        },
        nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}},
        nodes near coords custom=5e5,
        % ---------------------------------------------------------------------
    ]
        % adjusted `coordinates` to `table`
        \addplot table [x=x,y expr=\coordindex] {\MyData};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2
  • This is a great solution. I had something like this in mind, but no idea how to implement it.
    – Oliver
    May 14, 2021 at 13:10
  • 1
    Then I am happy that I provided it. May 14, 2021 at 14:26

You must log in to answer this question.

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