# Create a coordinate system or diagram from values in an existing table

I have a table in a document that looks like this:

\documentclass{scrreprt}
\usepackage{array}
\begin{document}
\begin{table}[htpb]
\centering
\captionabove{occurrence of X by year}
\begin{tabular}{||c|c|c|c|c|c|c|c|c||}
\hline
1915-1991 & 1992-1998 & 1999 & 2000 & 2001 & 2002 & 2003 & 2004 & 2005\\
0 & 8 & 7 & 12 & 24 & 32 & 35 & 44 & 65\\
\hline
2006 & 2007 & 2008 & 2009 & 2010 & 2011 & 2012 & 2013 & 2014\\
79 & 106 & 126 & 147 & 158 & 150 & 170 & 205 & 219\\
\hline
\end{tabular}
\end{table}
\end{document}


Is it possible to take the exact same values and elegantly put them in a coordinate system where the horizontal axis is year and the vertical axis is occurrence, or in a bar graph?

Or, what would be the best (understandable, eye-pleasing) way in a document to convey the information from this table?

• Two things that came to my mind: I would prefer your table to be transposed, that is having two columns instead of two rows. (My opinion :)) Second, my idea for solving your problem would be to export your data to a textfile (.csv for example) and use some packages to produce the table and your bar graph. Maybe csvsimple might do the former, while tikz should be able to do the latter - I don't know for sure, though... – cauchy42 Sep 16 '15 at 9:04
• @cauchy42 Problem with the two columns instead of two rows: I have several similar tables, and some of them have more years, but they all have only one value per year (or year group). So they’d take up a large vertical space if ordered vertically. – Philipp Sep 16 '15 at 13:39
• @cauchy42 Thanks, I’ll take a look at how to use csvsimple. – Philipp Sep 16 '15 at 13:39

This is not very eye-pleasing, but a point to start:

\documentclass{article}

\usepackage{filecontents}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{pgfplots}

\begin{filecontents*}{mytab.csv}
Year, Data
1915, 0
2002, 32
\end{filecontents*}

\begin{document}

\csvautotabular{mytab.csv}

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

\addplot table [x=Year, y=Data, col sep=comma] {mytab.csv};
\end{axis}
\end{tikzpicture}

\end{document}


Major problem with this code is, that you cannot have year ranges like 1915-1991 in the table, as tikz is interpreting these as floating point numbers. There is surely a way around this, but I don't know tikz good enough to tell.

• Thanks for your answer! Your example looks simpler than I thought it’d have to be. Maybe if we made tikz treat 1915-1991 as a string, it could work? I’m not sure how that would be done, though. – Philipp Sep 16 '15 at 13:44
• @Philipp you might possibly have a third column with tick labels, and the first column contain only the start year (or just an integer)... – cauchy42 Sep 16 '15 at 13:49