Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot mean and standard deviation

I have several values of a function at different x points. I want to plot the mean and std in python, like the answer of this SO question. I know this must be easy using matplotlib, but I have no idea of the function's name that can do that. Does anyone know it?

enter image description here

like image 525
teaLeef Avatar asked Mar 18 '14 14:03

teaLeef


People also ask

What kind of graph do you use for mean and standard deviation?

The bell curve (what statisticians call a “normal distribution“) is commonly seen in statistics as a tool to understand standard deviation. The following graph of a normal distribution represents a great deal of data in real life. The mean, or average, is represented by the Greek letter μ, in the center.

Can you make a boxplot with mean and standard deviation?

If we don't have whole data but mean and standard deviation are available then the boxplot can be created by finding all the limits of a boxplot using mean as a measure of central tendency.


2 Answers

plt.errorbar can be used to plot x, y, error data (as opposed to the usual plt.plot)

import matplotlib.pyplot as plt import numpy as np  x = np.array([1, 2, 3, 4, 5]) y = np.power(x, 2) # Effectively y = x**2 e = np.array([1.5, 2.6, 3.7, 4.6, 5.5])  plt.errorbar(x, y, e, linestyle='None', marker='^')  plt.show() 

plt.errorbar accepts the same arguments as plt.plot with additional yerr and xerr which default to None (i.e. if you leave them blank it will act as plt.plot).

Example plot

like image 200
Ffisegydd Avatar answered Oct 05 '22 23:10

Ffisegydd


You may find an answer with this example : errorbar_demo_features.py

""" Demo of errorbar function with different ways of specifying error bars.  Errors can be specified as a constant value (as shown in `errorbar_demo.py`), or as demonstrated in this example, they can be specified by an N x 1 or 2 x N, where N is the number of data points.  N x 1:     Error varies for each point, but the error values are symmetric (i.e. the     lower and upper values are equal).  2 x N:     Error varies for each point, and the lower and upper limits (in that order)     are different (asymmetric case)  In addition, this example demonstrates how to use log scale with errorbar. """ import numpy as np import matplotlib.pyplot as plt  # example data x = np.arange(0.1, 4, 0.5) y = np.exp(-x) # example error bar values that vary with x-position error = 0.1 + 0.2 * x # error bar values w/ different -/+ errors lower_error = 0.4 * error upper_error = error asymmetric_error = [lower_error, upper_error]  fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True) ax0.errorbar(x, y, yerr=error, fmt='-o') ax0.set_title('variable, symmetric error')  ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o') ax1.set_title('variable, asymmetric error') ax1.set_yscale('log') plt.show() 

Which plots this:

enter image description here

like image 37
Kiwi Avatar answered Oct 05 '22 22:10

Kiwi