# Guide to draw charts (basic, pie, bar) from data

I went through the examples of using PSTricks and TikZ for drawing pie and bar chart. They looked quiet complicated to me. Is there any simple guide (blogs/text) to drawing these charts using PDFLaTeX. Learning to use gnuplot will be too much for a small presentation I am trying to create!

• You could have a look at package pgfplots – Johannes_B Feb 1 '14 at 9:17

The pgfplots manual gives a lot of information and tutorials- you'll also find a lot of great examples on this site.

Here's a humble tutorial on how I created the following bar graph, which I used in answering How to clarify and enliven a dense table using the wonderful pgfplots package; the complete code is given at the end.

The first objective is to read the data- because the data came from a LaTeX table, we can tell pgfplots to separate columns by & and rows by \\. There are a lot of other options for reading data, including Comma Separated Value files (.csv), for example.

\pgfplotstableread[row sep=\\,col sep=&]{
interval & carT & carD & carR \\
0--2     & 1.2  & 0.1  & 0.2  \\
2--5     & 12.8 & 3.8  & 4.9  \\
5--10    & 15.5 & 10.4 & 13.4 \\
10--20   & 14.0 & 17.3 & 22.2 \\
20--50   & 7.9  & 21.1 & 27.0 \\
50+      & 3.0  & 22.3 & 28.6 \\
}\mydata


We can now access this in pgfplots commands using \mydata.

# A basic plot

We can make a very basic bar chart by using

\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={0--2,2--5,5--10,10--20,20--50,50+},
xtick=data,
]
\end{axis}
\end{tikzpicture}


which gives

Notice that we had to tell it to use the symbolic x coords so that it knew to use the values from the interval column on the horizontal axis.

We can easily add the other bars by using more addplot commands

\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={0--2,2--5,5--10,10--20,20--50,50+},
]
\end{axis}
\end{tikzpicture}


which gives

Notice that we kept x=interval and changed the y= to suit the appropriate columns.

We can add a few more details such as the numbering near the top of each bar, and a legend by using nodes near coords and \legend respectively

\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={0--2,2--5,5--10,10--20,20--50,50+},
xtick=data,
nodes near coords,
]
\legend{Trips, Distance, Energy}
\end{axis}
\end{tikzpicture}


This gives

# Final tweaks

It's a little cramped together, so let's specify the width, height, and viewing window; we can also move the legend around and specify a label for the y-axis

\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=.5cm,
width=\textwidth,
height=.5\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={0--2,2--5,5--10,10--20,20--50,50+},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
ymin=0,ymax=35,
ylabel={\%},
]
\legend{Trips, Distance, Energy}
\end{axis}
\end{tikzpicture}


Here's the result

There are lot of other keys that you can use to tweak the look and feel of your chart- explore the manual and this site for more information.

# Complete MWE

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}

interval & carT & carD & carR \\
0--2     & 1.2  & 0.1  & 0.2  \\
2--5     & 12.8 & 3.8  & 4.9  \\
5--10    & 15.5 & 10.4 & 13.4 \\
10--20   & 14.0 & 17.3 & 22.2 \\
20--50   & 7.9  & 21.1 & 27.0 \\
50+      & 3.0  & 22.3 & 28.6 \\
}\mydata

\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=.5cm,
width=\textwidth,
height=.5\textwidth,
legend style={at={(0.5,1)},
anchor=north,legend columns=-1},
symbolic x coords={0--2,2--5,5--10,10--20,20--50,50+},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
ymin=0,ymax=35,
ylabel={\%},
]