Building bar charts is a useful skill for engineers.

Import matplotlib and numpy


In [2]:
import matplotlib.pyplot as plt
import numpy as np

#if using a jupyter notebook
%matplotlib inline

Coefficient of thermal expansion of three metals (units: / °C)

Sample Aluminum Copper Steel
1 6.4e-5 4.5e-5 3.3e-5
2 3.01e-5 1.97e-5 1.21e-5
3 2.36e-5 1.6e-5 0.9e-5
4 3.0e-5 1.97e-5 1.2e-5
5 7.0e-5 4.0e-5 1.3e-5
6 4.5e-5 2.4e-5 1.6e-5
7 3.8e-5 1.9e-5 1.4e-5
8 4.2e-5 2.41e-5 1.58e-5
9 2.62e-5 1.85e-5 1.32e-5
10 3.6e-5 3.3e-5 2.1e-5

We'll put this data into three different numpy arrays, one array for each metal. Notice the syntax np.array([ ... ]) has a parenthesis ( followed by a square bracket [. We are passing a Python list, [ denoted with square brackets ] into a the numpy array function (argument enclosed in parenthesis).


In [3]:
# Enter in the raw data
aluminum = np.array([6.4e-5 , 3.01e-5 , 2.36e-5, 3.0e-5, 7.0e-5, 4.5e-5, 3.8e-5, 4.2e-5, 2.62e-5, 3.6e-5])
copper = np.array([4.5e-5 , 1.97e-5 , 1.6e-5, 1.97e-5, 4.0e-5, 2.4e-5, 1.9e-5, 2.41e-5 , 1.85e-5, 3.3e-5 ])
steel = np.array([3.3e-5 , 1.2e-5 , 0.9e-5, 1.2e-5, 1.3e-5, 1.6e-5, 1.4e-5, 1.58e-5, 1.32e-5 , 2.1e-5])

In [9]:
# Calculate the average
Aluminum_mean = np.mean(aluminum)
Copper_mean = np.mean(copper)
Steel_mean = np.mean(steel)
CTEs =np.array([Aluminum_mean, Copper_mean, Steel_mean])
x_pos =['Al','Cu','Steel']

Now it's time to build the plot. We are going to build a bar chart with three different bars, one bar for each material: Aluminum, Copper and Steel.

First we will create a figure object called fig and an axis object in that figure called ax using matplotlib's plt.subplots() function. Everything in our plot will be added to the ax (axis) object. Next we put a bar chart on our ax (axis) with the ax.bar() method. Note the arguments that go into this method: (x_pos, CTEs, yerr=error). x_pos is the array with the count of the number of bars. CTEs is our array which contains the means or heights of the bars. yerr=error sets the heights of the error bars and the standard deviations. The subsequent arguments (align='center', alpha=0.5, ecolor='black', capsize=10) styles the plot.

We'll put a label on the y-axis with the title "Coefficient of thermal expansion (°C-1)" using ax.set_ylabel. We use ax.set_xticks() to feed in our number array to set the bars as numbers 1, 2, 3. Then we add labels to these numbered bars with ax.set_ticklabels(). ax.set_title() and ax.yaxis.grid(True) adds a title and horizontal grid lines.

Finally, we we'll save the figure to a file called _bar_plot_with_errorbars.png using matplotlib's plt.savefig() function. The plt.thight_layout() line ensures that the labels for our bars and axis don't get cut off and are visible.


In [12]:
# Build the plot
fig, ax = plt.subplots()
ax.bar(x_pos, CTEs, align='center', alpha=0.5)
ax.set_ylabel('Coefficient of Thermal Expansion ($\degree C^{-1}$)')

ax.set_xticklabels(materials)
ax.set_title('Coefficent of Thermal Expansion (CTE) of Three Metals')
ax.yaxis.grid(True)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-efdbb5e26a97> in <module>()
      4 ax.set_ylabel('Coefficient of Thermal Expansion ($\degree C^{-1}$)')
      5 
----> 6 ax.set_xticklabels(materials)
      7 ax.set_title('Coefficent of Thermal Expansion (CTE) of Three Metals')
      8 ax.yaxis.grid(True)

NameError: name 'materials' is not defined

In [ ]: