Python 绘制均值和标准差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22481854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:01:11  来源:igfitidea点击:

Plot mean and standard deviation

pythonmatplotlibplot

提问by teaLeef

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?

我在不同的 x 点有多个函数值。我想在 python 中绘制均值和标准差,就像这个 SO question的答案一样。我知道使用 matplotlib 这一定很容易,但我不知道可以做到这一点的函数名称。有人知道吗?

enter image description here

在此处输入图片说明

采纳答案by Ffisegydd

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

plt.errorbar可用于绘制 x、y、误差数据(与通常的相反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.errorbaraccepts the same arguments as plt.plotwith additional yerrand xerrwhich default to None (i.e. if you leave them blank it will act as plt.plot).

plt.errorbar接受相同的参数plt.plot与另外yerrxerr其默认为无(即如果你保留空白它将作为plt.plot)。

Example plot

示例图

回答by Kiwi

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

你可以通过这个例子找到答案: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

在此处输入图片说明