Making bar charts with error bars

In this recipe, we will show how to create bar charts and how to draw error bars.

Getting ready

To visualize uncertainty of measurement in our dataset or to indicate the error, we can use error bars. Error bars can easily give an idea of how error free the dataset is. They can show one standard deviation, one standard error, or 95 percent confidence interval. There is no standard here, so always explicitly state what values (errors) error bars display. Most papers in the experimental sciences should contain error bars to present accuracy of the data.

How to do it...

Even though just two parameters are mandatory—left and height— we often want to use more than that. Here are some parameters we can use:

  • width: This gives the width of the bars. The default value is 0.8.
  • bottom: If bottom is specified, the value is added to the height. The default is None.
  • edgecolor: This gives the color of the bar edges.
  • ecolor: This specifies the color of any error bar.
  • linewidth: This gives width of bar edges; special values are None (use defaults) and 0 (when bar edges are not displayed).
  • orientation: This has two values vertical and horizontal.
  • xerr and yerr: These are used to generate error bars on the bar chart.

Some optional arguments (color, edgecolor, linewidth, xerr, and yerr) can be single values or sequences with the same length as the number of bars.

How it works...

Let's illustrate this using an example:

import numpy as np
import matplotlib.pyplot as plt

# generate number of measurements
x = np.arange(0, 10, 1)

# values computed from "measured"
y = np.log(x)

# add some error samples from standard normal distribution
xe = 0.1 * np.abs(np.random.randn(len(y)))

# draw and show errorbar
plt.bar(x, y, yerr=xe, width=0.4, align='center', ecolor='r', color='cyan', label='experiment #1');

# give some explanations
plt.xlabel('# measurement')
plt.ylabel('Measured values')
plt.title('Measurements')
plt.legend(loc='upper left')

plt.show()

The preceding code will plot the following diagram:

How it works...

To be able to plot an error bar, we needed to have some measures (x); for every measure computed (y), we introduced errors (xe).

We used NumPy to generate and compute values; standard distributions are good enough for demonstration purposes, but if you happen to know your data distribution in advance, you can always make some prototype visualizations and try out different layouts to find the best options to present information.

Another interesting option to use if we are preparing visualizations for a black-and-white medium is hatch; it can have the following values:

Hatch value

Description

/

Diagonal hatching

Back diagonal

|

Vertical hatching

-

Horizontal

+

Crossed

x

Crossed diagonal

o

Small circle

0

Large circle

.

Dot pattern

*

Star pattern

There's more...

What we have just used are error bars known as symmetrical error bars. If the nature of our dataset is such that errors are not the same in both directions (negative and positive), we can also specify them separately using asymmetrical error bars.

All we have to do differently is to specify xerr or yerr using a two-element list (such as a 2D array), where the first list contains values for negative errors and the second one for positive errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.151