[Tex/LaTex] Guide to draw charts (basic, pie, bar) from data

bar chartpgf-pietikz-pgf

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!

Best Answer

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.

bargraph

Reading the data

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,
        ]
        \addplot table[x=interval,y=carT]{\mydata};
    \end{axis}
\end{tikzpicture}

which gives

first attempt

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.

Adding more bars

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+},
        ]
        \addplot table[x=interval,y=carT]{\mydata};
        \addplot table[x=interval,y=carD]{\mydata};
        \addplot table[x=interval,y=carR]{\mydata};
    \end{axis}
\end{tikzpicture}

which gives

more bars

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

Adding more details

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,
        ]
        \addplot table[x=interval,y=carT]{\mydata};
        \addplot table[x=interval,y=carD]{\mydata};
        \addplot table[x=interval,y=carR]{\mydata};
        \legend{Trips, Distance, Energy}
    \end{axis}
\end{tikzpicture}

This gives

nodes near coords and legend

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={\%},
        ]
        \addplot table[x=interval,y=carT]{\mydata};
        \addplot table[x=interval,y=carD]{\mydata};
        \addplot table[x=interval,y=carR]{\mydata};
        \legend{Trips, Distance, Energy}
    \end{axis}
\end{tikzpicture}

Here's the result

final 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}

\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

\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={\%},
        ]
        \addplot table[x=interval,y=carT]{\mydata};
        \addplot table[x=interval,y=carD]{\mydata};
        \addplot table[x=interval,y=carR]{\mydata};
        \legend{Trips, Distance, Energy}
    \end{axis}
\end{tikzpicture}
\end{document}
Related Question