23

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!

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

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}
| improve this answer | |
  • I'm sorry for reviving an old topic, but the description of the table goes on the preamble of the document? – Tuma Jun 29 '16 at 1:06
  • if x is not interval, (name, for example), what should I put into table[x=interval,y=carR]{\mydata}; – Haha TTpro Aug 30 '17 at 13:53
  • @Tuma Yes, that is recommended. – Duncan Mar 26 '18 at 18:36
  • @HahaTTpro Then table[x=name,y=othername]{\morenames}; – Duncan Mar 26 '18 at 18:36
1

PGF has a wonderful site with lots of examples and explanations (Look at the bottom of the site). Here. TEXample is a page with structured examples. Here. PSTricks has a page with examples and 2D/3D gallery. Here. Also on http://tex.stackexchange.com you can find many references. Use the search function with your keywords.

| improve this answer | |

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.