Gri*_*iff 5 python numpy scatter histogram
我有一堆数据分散x,y.如果我想根据x将它们分开并将误差条等于它们的标准偏差,我将如何去做呢?
我在python中唯一知道的是循环遍历x中的数据并根据bin(max(X)-min(X)/ nbins)对它们进行分组,然后循环遍历这些块以找到std.我确信有更快的方法可以用numpy做到这一点.
我希望它看起来类似于"vert symmetric":http://matplotlib.org/examples/pylab_examples/errorbar_demo.html
Jai*_*ime 14
你可以用你的数据np.histogram
.我正在重复使用其他答案中的代码来计算binned的平均值和标准偏差y
:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.sin(2*np.pi*x) + 2 * x * (np.random.rand(100)-0.5)
nbins = 10
n, _ = np.histogram(x, bins=nbins)
sy, _ = np.histogram(x, bins=nbins, weights=y)
sy2, _ = np.histogram(x, bins=nbins, weights=y*y)
mean = sy / n
std = np.sqrt(sy2/n - mean*mean)
plt.plot(x, y, 'bo')
plt.errorbar((_[1:] + _[:-1])/2, mean, yerr=std, fmt='r-')
plt.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10468 次 |
最近记录: |