2

I’m trying to have a diagram that shows the information produced by this MWE:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Jahr, Treffer
1995-2010, 4
2005, 3
2006, 7
2007, 15
2008, 68
\end{filecontents*}
\usepackage{array}
\usepackage{csvsimple}    
\usepackage{pgfplots}
\begin{document}
\csvautotabular{data.csv}
\end{document}

Instead of \csvautotabular{data.csv}, I tried this:

\begin{tikzpicture}
  \begin{axis}[
    ybar,
    ]

    \addplot table [x=Jahr, y=Treffer, col sep=comma] {data.csv};
  \end{axis}
\end{tikzpicture}

but get the error

! Package PGF Math Error: Could not parse input '1995-2010' as a
floating point number, sorry. The unreadable part was near '-2010'..

Is there a good way around this, like treating the 1995-2010 as a string and pretending it’s just another single year?

2

With the idea from this answer, you can insert a third column "Label" in your .csv which keeps the label strings, and just have the beginning of the year range as x-values for the plot:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Jahr, Treffer, Label
1995, 4, 1995-2004
2005, 3, 2005
2006, 7, 2006
2007, 15, 2007
2008, 68, 2008
\end{filecontents*}
\usepackage{array}
\usepackage{csvsimple}    
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\csvautotabular{data.csv}


\begin{tikzpicture}
  \begin{axis}[
    ybar,
    xtick=data,
    xticklabels from table={data.csv}{Label}
    ]

    \addplot table [x=Jahr, y=Treffer, col sep=comma] {data.csv};
  \end{axis}
\end{tikzpicture}

\end{document}

However, the labels are quite long and thus overlapping, so apply some make-up to your plot.

EDIT: In particular I like the idea of having horizontal bars:

\begin{tikzpicture}
  \begin{axis}[
    xbar,
    ytick=data,
    yticklabels from table={data.csv}{Label}
    ]

    \addplot table [y=Jahr, x=Treffer, col sep=comma] {data.csv};
  \end{axis}
\end{tikzpicture}
  • Thanks a bunch, again, cauchy42! Your answer perfectly solved my problem. Concerning the overlapping years, I’ll take a look at the suggested question & answer. – Philipp Sep 19 '15 at 16:12

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.