< Previous Page | Home Page | Next Page >
Next to line and scatter plots, bar charts are a great way of visualizing information. A bar chart is created using plt.bar()
. The bars are positioned at x with the given align\ment. Their
dimensions are given by width and height. The vertical baseline
is bottom (default 0).
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
data = [1,2,3,4]
xvalues = range(len(data))
plt.bar(x= xvalues,
height= data,
width = -0.3,
bottom= 1,
align="edge")
# once again, the documentation lists several possible parameter combinations
plt.bar?
Obviously we can also plot several different bars within one bar chart. This is a great tool for visualizing some sort of comparison.
To make this work, we simply plot two sets of bar charts but add the width of the first bar chart to the x-value of the second bar chart. This way both bar charts are directly next to each other.
linear_data = [1,2,3,4]
exponential_data = [ x**2 for x in linear_data ]
xvals = range(len(linear_data))
plt.bar(xvals, linear_data, width = 0.3)
# plot another set of bars, adjusting the new xvals to make up for thefirst set of bars plotted
new_xvals = []
for item in xvals:
new_xvals.append(item+0.3)
plt.bar(new_xvals, exponential_data, width = 0.3 ,color='red')
For any scientific measurement, accurate accounting for errors is nearly as important, if not more important, than accurate reporting of the number itself. For example, imagine that some study results in the following bar charts.
linear_data = [1,2,3,4]
xvals = range(len(linear_data))
# This will plot a new set of bars with errorbars using the list of random error values
plt.bar(xvals, linear_data, width = 0.3)
Now however, it could be the cas that there might be potential deviation between the results of our study and the findings of another study. This difference can then be plotted using the yerr
parameter.
linear_data = [1,2,3,4]
xvals = range(len(linear_data))
# specify by which amount the data points may deviate
linear_err = [5,2,10,6]
# Plot a new set of bars with errorbars using the list of error values
plt.bar(xvals, linear_data, width = 0.3, yerr=linear_err)
In other cases, it might be useful to not visualize different bar types next to each other but stack them up. This can be done by plotting two plt.bar()
and using the bottom
parameter within the second bar chart to specify that the first bar chart shall be used as the "ground".
linear_data = [1,2,3,4]
exponential_data = [1,4,9,16]
xvals = range(len(linear_data))
# create normal bar chart with blue bars
plt.bar(xvals, linear_data, width = 0.3, color='b')
#create second bar chart with red bars, the linear data is specified as the new bottom
plt.bar(xvals, exponential_data, width = 0.3, bottom=linear_data, color='r')
Of course, it is also possible to change the axis of the individual bar's root. To do so we merely changeplt.bar()
with plt.barh()
linear_data = [1,2,3,4]
exponential_data = [ x**2 for x in linear_data]
xvals = range(len(linear_data))
#use plt.barh() instead of plt.bar()
plt.barh(xvals, linear_data, height = 0.3, color='b')
plt.barh(xvals, exponential_data, height = 0.3, left=linear_data,color='r')
< Previous Page | Home Page | Next Page >