Beschriftung des Diagramms wird nicht richtig ausgegeben Thema ist als GELÖST markiert

Tabellen und Grafiken erstellen und anordnen


Gast

Beschriftung des Diagramms wird nicht richtig ausgegeben

Beitrag von Gast »

Moin

Ich habe ein Balkendiagramm erstellt. Nun hätte ich gerne, dass die Jahreszahlen unten dargestellt werden (Beschriftung der Balken). Allerdings wird dies nicht so ausgegeben.

Hat mir jemand einen Tipp, was ich falsch mache oder was das Problem ist?
\documentclass[12pt,a4paper]{report} 
\usepackage{pgfplotstable}% lädt u.a. auch pgfplots,tikz, xcolor, ... 
\pgfplotsset{compat=newest} 
\usetikzlibrary{arrows.meta} 
\definecolor{myblue}{HTML}{92dcec} 
\usepackage{caption} 
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\begin{document} 
\noindent 
\begin{minipage}{\linewidth}
\begin{tikzpicture} 

\pgfplotstableread{ 
Jahr          I    
{2012}		240
{2013}		256 
{2014}		270
{2015}		294   
}{\data}; 

\begin{axis}[ 
  width=\linewidth-45pt, 
  height=6cm,  
  scale only axis, 
  axis lines=left, 
  x axis line style={|[left]-|[right]}, 
  y axis line style={|[right]-|[left]}, 
  ylabel=Anzahl, 
  ymin=0,ymax=300, 
  ytick={50,100,150,200,250,300}, 
  ymajorgrids, 
  xlabel=Jahr, 
  xtick={0,...,4},
  xticklabels={{2012},{2013},{2014},{2015}}, 
  xticklabel style={anchor=east,rotate=45,align=center}, 
  ybar=0pt, 
  bar width=2cm, 
  enlarge x limits={abs=2.5cm}, 
  nodes near coords, 
  legend style={yshift=1cm, column sep=1mm},
  legend cell align=left
] 
\addplot[fill=myblue]table[y=I]{\data}; 
\end{axis} 
\end{tikzpicture} 
\captionof{figure}{Beschreibung}
\end{minipage} 
\end{document}

esdd
Forum-Meister
Forum-Meister
Beiträge: 2561
Registriert: So 7. Feb 2010, 16:36

Beitrag von esdd »

\addplot[fill=myblue]table[y=I]{\data}; 
nutzt die erste Spalte als x-Wert. Du kannst jetzt entweder für die Ticks
xtick={2012,...,2015},


verwenden und damit folgenden Code nutzen:
\documentclass[12pt,a4paper]{report} 
\usepackage{pgfplotstable}% lädt u.a. auch pgfplots,tikz, xcolor, ... 
\pgfplotsset{compat=newest} 
\usetikzlibrary{arrows.meta} 
\definecolor{myblue}{HTML}{92dcec} 
\usepackage{caption} 
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\begin{document} 
\noindent 
\begin{minipage}{\linewidth}
\begin{tikzpicture} 

\pgfplotstableread{ 
Jahr          I    
{2012}      240
{2013}      256 
{2014}      270
{2015}      294   
}{\data}; 

\begin{axis}[ 
  width=\linewidth-45pt, 
  height=6cm,  
  scale only axis, 
  axis lines=left, 
  x axis line style={|[left]-|[right]}, 
  y axis line style={|[right]-|[left]}, 
  ylabel=Anzahl, 
  ymin=0,ymax=300, 
  ytick={50,100,150,200,250,300}, 
  ymajorgrids, 
  xlabel=Jahr, 
  xtick={2012,...,2015},
  xticklabels from table={\data}{Jahr},
  xticklabel style={anchor=east,rotate=45,align=center}, 
  ybar=0pt, 
  bar width=2cm, 
  enlarge x limits={abs=2.5cm}, 
  nodes near coords, 
  legend style={yshift=1cm, column sep=1mm},
  legend cell align=left
] 
\addplot[fill=myblue]table[y=I]{\data}; 
\end{axis} 
\end{tikzpicture} 
\captionof{figure}{Beschreibung}
\end{minipage} 
\end{document}
Oder Du verwendest
\addplot[fill=myblue]table[x expr=\coordindex,y=I]{\data};
damit der Koordinatenindex als x-Wert genutzt wird.
\documentclass[12pt,a4paper]{report} 
\usepackage{pgfplotstable}% lädt u.a. auch pgfplots,tikz, xcolor, ... 
\pgfplotsset{compat=newest} 
\usetikzlibrary{arrows.meta} 
\definecolor{myblue}{HTML}{92dcec} 
\usepackage{caption} 
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc} 
\begin{document} 
\noindent 
\begin{minipage}{\linewidth}
\begin{tikzpicture} 

\pgfplotstableread{ 
Jahr          I    
{2012}      240
{2013}      256 
{2014}      270
{2015}      294   
}{\data}; 

\begin{axis}[ 
  width=\linewidth-45pt, 
  height=6cm,  
  scale only axis, 
  axis lines=left, 
  x axis line style={|[left]-|[right]}, 
  y axis line style={|[right]-|[left]}, 
  ylabel=Anzahl, 
  ymin=0,ymax=300, 
  ytick={50,100,150,200,250,300}, 
  ymajorgrids, 
  xlabel=Jahr, 
  xtick={0,...,4},
  xticklabel style={anchor=east,rotate=45,
  	/pgf/number format/set thousands separator={}
  },
  ybar=0pt, 
  bar width=2cm, 
  enlarge x limits={abs=2.5cm}, 
  nodes near coords, 
  legend style={yshift=1cm, column sep=1mm},
  legend cell align=left
] 
\addplot[fill=myblue]table[x expr=\coordindex,y=I]{\data}; 
\end{axis} 
\end{tikzpicture} 
\captionof{figure}{Beschreibung}
\end{minipage} 
\end{document}

Gast

Beitrag von Gast »

Danke!!
Funktioniert wunderbar! :D

Antworten