Skip to content

Bar Charts

Bar Charts

Bar charts, also called bar graphs, or bar plots are constructed with Matplotlib's pyplot library. To construct a bar chart with Matplotlib, first import Matplotlib. If using a Jupyter notebook include the line %matplotlib inline.

In [1]:
import matplotlib.pyplot as plt
# if using a Jupyter notebook, inlcude
%matplotlib inline

To create a bar chart with Matplotlib, we need some data. In this example, the data is from a set of tensile strength measurements. Tensile strength is a material property that describes how much stress a material withstand before fracture. Different materials have different tensile strengths. The higher the tensile strength, the stronger the material.

A list of 3 metals and their corresponding tensile strength is below:

  • Brass = 125 MPa
  • Aluminum = 276 MPa
  • Steel = 345 MPa

To build the bar chart, we create a list of bar heights. The bar heights are the tensile strengths of the three metals. We also need a list of bar positions. Specifying bar positions seems strange, but the plt.bar() method needs to know where along the x-axis to put the bars. The plt.bar() method requires two positional arguments:

plt.bar([list of bar positions], [list of bar heights])

An error results if only a list of bar heights is specified.

In [2]:
# define bar heights and bar positions
heights = [125, 276, 345]
x_pos = [1, 2, 3]

Next, build the plot by passing in the list of bar position x_pos and list of bar heights heights as positional arguments to the plt.bar() function. These positional arguments must be specified in the proper order.
In [3]:
# Build the plot
plt.bar(x_pos,heights)

plt.show()

Let's add some details to the bar plot. We can specify bar labels and where to put the labels. The opacity of the bars can be decreased which makes the bars slightly transparent. Besides changing opacity, many other keyword arguments can be included in plt.bar() function call.

A table of a few of the plt.bar() keyword arguments is below:

bar plot feature keyword argument Example
bar face color color= plt.bar(x_pos, heights, color='g')
bar opacity alpha= plt.bar(x_pos, heights, alpha=0.5)
bar outline color edgecolor= plt.edgecolor(x_pos, heights, edgecolor='k')
bar outline width linewidth= plt.bar(x_pos, heights, linewidth=3)
y-error bar heights yerr= plt.bar(x_pos, heights, yerr=[0.1, 0.3, 0.2])
error bar cap width capsize= plt.bar(x_pos, heights, capsize=5)

Assuming x_pos is a list of x-positions for the bars, and heights is a list of bar heights, an example plt.bar() function call might be:

plt.bar(x_pos, heights,
        color='b',
        edgecolor='k',
        linewidth=4,
        yerr=[0.1, 0.3, 0.1],
        capsize=5)

Labels can be added to the bar plot with the same syntax used to customize line plots. These function calls include:

  • plt.title('Plot title')
  • plt.xlabel('x-axis label')
  • plt.ylabel('y-axis label')
  • plt.grid(axis='y')
    In [4]:
    import matplotlib.pyplot as plt
    # if using a Jupyter notebook, include:
    %matplotlib inline

materials = ['Brass', 'Aluminum', 'Steel'] heights = [125, 276, 345] x_pos = [1, 2, 3]

plt.bar(x_pos, heights, alpha=0.5)

plt.title('Tensile Strength of Three Metals') plt.xlabel('Metals') plt.ylabel('Tensile Strength (MPa)') plt.xticks([1,2,3], materials) plt.grid(axis='y')

plt.show()