a pen over a paper with a bar chart

Creating Bar Graphs in LaTex

LaTex is a highly flexible typesetting program that seems to be capable of almost anything. Here, we will learn how to make bar graphs use this language.

Simple Bar Graph

Below is the code and the bar graph for simple bar graph.

Here is what is happening in the code

  1. Line 1-2 we declare our document class and load the only package we need which is pgfplots.
  2. Line 3-5 we declare the various environments needed
  3. Line 6-12 are all the details we need to create the axis on which our data will be.
  4. Line 7 gives the title of the graph
  5. Line 8 makes the graph a bar graph
  6. Line 9 places the numbers above the bars in the bar graph to make it easier to read. This line also controls how thick the bars are
  7. Line 10 sets the names for the x-axis
  8. Line 11 makes sure each bar only has one name
  9. Line 13 creates the plot. Inside the curly braces you have in parentheses the name of the group and the frquency.
  10. The rest of the code closes the enviornments

Multiple Bars

In the code below we will add several bar graphs to one plot and also add a legend. The code is mostly the same below.

The new information is in lines 12-15. These lines contain the information for the legend. The at argument tells where to put the legend at, anchor tells LaTex how to hold it and the column sep determines the width of the column.


The next important information is in lines 19-23. Here is were we add additional bars. The arguments in side still tell LaTex what color the numbers above the bar should be as well as the color of the bar graphs. The coordinates is were you can hand code the values for the x axis.

The final example will use raw data already available in LaTex to make a bar graph.

Using Data

Below is the code followed by the bar plot

New information is found first in lines 4-9. Here we use a command called “pgfplotstableread” in order to create the table. The table is found in lines 5-9. The first row sets the columns of the table and lines 6-8 is our actual data. In line 9 we give our table a name called “mydata.” It is important to note that this table will not appear in your pdf. Rather this table is just for storing information.If you want to show the table you need to make a table the traditional way.

Line 15 has the names of the groups in our data which were animal names. The next major change is in line 18. Here, we use the “addplot” command to add a table with the x as animal and the y as weight. Next to this information in curly braces is the name of the dateset called mydata.

If you look at the table you can see that we have information on the animals weight and speed but the bar plot only shows weight. In Line 19 there was some code in red which means that it was not compiled. By removing the percent sign we can run all the code and get the following.

The idea of adding to a bar plot remains the same. Just create another instance of the command and run it.


Making bar plots in LaTex is another convenient tool. It allows you to manipulate the data inside LaTex rather then having to pull images from your folder. If your data is stable and not going to change this might be worth while.

Leave a Reply