Seite 1 von 1

Balkendiagramm Formatierung

Verfasst: Mo 2. Feb 2026, 14:58
von olko
Ich habe bei dem folgenden Diagramm das Problem, dass links und rechts Nullen auftauchen. Mir ist auch klar wieso. Lasse ich die Dummies weg, dann sind die Balken abgeschnitten oder die Beschriftung ist verrutscht. Ich hoffe, dass jemand eine Lösung hat.
\begin{center}
\begin{tikzpicture}
  \begin{axis}[
    title={Notenverteilung in einer Klasse},
    ybar,
    bar width=15pt,
    ymin=0,
    ymax=10,
    width=13cm,
    height=6cm,
    symbolic x coords={Xleft, A, B, C, D, E, F, Xright},
    xtick=data,
    xticklabels={, A, B, C, D, E, F, }, % Dummy-Beschriftungen leer
    ylabel={Anzahl},
    axis lines=left,
    grid=major,
    major grid style={gray!30},
    nodes near coords,
    every node near coord/.append style={
      font=\small,
      color=blue
    },
    % Nullen weiß färben – Trick!
    every node near coord/.append style={
      /pgfplots/point meta post/.code={
        \pgfmathparse{\pgfplotspointmeta==0 ? "white" : "blue"}
        \edef\tempcolor{\pgfmathresult}
        \tikzset{every node near coord/.style={font=\small, color=\tempcolor}}
      }
    }
  ]

  \addplot+[
    draw=blue!80!black,
    fill=blue!30
  ] coordinates {
    (Xleft,0) (A,2) (B,5) (C,9) (D,7) (E,3) (F,1) (Xright,0)
  };

  \end{axis}
\end{tikzpicture}
\end{center}

Re: Balkendiagramm Formatierung

Verfasst: Mo 2. Feb 2026, 20:58
von Stefan Kottwitz
Hallo Olko,

Du kannst einfach einen separaten zweiten Plot ergänzen für die dummy-Einträge, und diesen ohne nodes near coords machen:
\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    title={Notenverteilung in einer Klasse},
    ybar,
    bar width=15pt,
    ymin=0,
    ymax=10,
    width=13cm,
    height=6cm,
    symbolic x coords={Xleft, A, B, C, D, E, F, Xright},
    xtick=data,
    xticklabels={, A, B, C, D, E, F, }, % Dummy-Beschriftungen leer
    ylabel={Anzahl},
    axis lines=left,
    grid=major,
    major grid style={gray!30},
    every node near coord/.append style={
      font=\small,
      color=blue
    },
  ]
  \addplot+[
    draw=blue!80!black,
    fill=blue!30,
    nodes near coords,
  ] coordinates {
   (A,2) (B,5) (C,9) (D,7) (E,3) (F,1)
  };
  \addplot+[
    draw=none,
    fill=none,
  ] coordinates {
    (Xleft,0) (Xright,0)
  };
  \end{axis}
\end{tikzpicture}
\end{document}

Re: Balkendiagramm Formatierung

Verfasst: Mi 4. Feb 2026, 13:14
von olko
Hallo Stefan,
danke für die Antwort. Das hatte ich tatsächlich auch schon gemacht. Allerdings sind dann die Bezeichnungen A, B, usw. verrutscht und sitzen nicht mehr unter den Balken. Oder wird das nur bei mir so angezeigt?

Re: Balkendiagramm Formatierung

Verfasst: Mi 4. Feb 2026, 13:45
von Stefan Kottwitz
Ah, nimm bar shift=0pt. Klicke dann mal auf "LaTeX-Ausgabe":
\documentclass[tikz,border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    title={Notenverteilung in einer Klasse},
    ybar,
    bar width=15pt,
    ymin=0,
    ymax=10,
    width=13cm,
    height=6cm,
    symbolic x coords={Xleft, A, B, C, D, E, F, Xright},
    xtick=data,
    xticklabels={A, B, C, D, E, F }, % Dummy-Beschriftungen leer
    ylabel={Anzahl},
    axis lines=left,
    grid=major,
    major grid style={gray!30},
    every node near coord/.append style={
      font=\small,
      color=blue
    },
    bar shift=0pt
  ]
  \addplot+[
    draw=blue!80!black,
    fill=blue!30,
    nodes near coords,
  ] coordinates {
   (A,2) (B,5) (C,9) (D,7) (E,3) (F,1)
  };
  \addplot+[
    draw=none,
    fill=none,
  ] coordinates {
    (Xleft,0) (Xright,0)
  };
  \end{axis}
\end{tikzpicture}
\end{document}
Stefan

Re: Balkendiagramm Formatierung

Verfasst: Do 5. Feb 2026, 11:32
von olko
Danke so sieht es gut aus. Wobei ich festgestellt habe, dass nicht jeder PDF-Viewer die Nullen anzeigt. Keine Ahnung wieso.
Aber mit bar shift sollte es klappen.