简体   繁体   English

使用 numpy 的标准偏差?

[英]Standard Deviation using numpy?

I'm trying to figure out how to plot the standard deviation with error bars and/or a gray area (sort of like a confidence interval) from my data.我试图从我的数据中弄清楚如何 plot 带有误差线和/或灰色区域(有点像置信区间)的标准偏差。 It contains data from multiple subjects across multiple days.它包含来自多个主题的多天数据。 I've seen a few lines of code that help explain but I'm having troubles trying to fit the code in. I understand the code uses numpy, but I've used matplot for most of this figure so I'm not sure how to translate it (still fairly new to this).我已经看到了几行有助于解释的代码,但我在尝试将代码放入其中时遇到了麻烦。我了解代码使用 numpy,但我在这个图中的大部分内容中都使用了 matplot,所以我不确定如何翻译它(对此仍然相当新)。

For further clarification: there are nine total subjects and each of them have an accuracy that ranges from ~50% - 100%.进一步澄清:总共有九个科目,每个科目的准确率范围为~50% - 100%。 The data is compiled in an excel that has a row for "Days" (1-22) and "Subject" (with their corresponding accuracy on the given day, ie, 50% on day 1, 65% day 2, etc).数据编译在 excel 中,其中有一行表示“天”(1-22)和“主题”(在给定的日期具有相应的准确度,即第 1 天为 50%,第 2 天为 65%,等等)。

Here is the lines of code I've found:这是我找到的代码行:

# Calculate the standard deviation of datasets
stdv_data=np.std(data)

# Create an error bar for each dataset
line_stdv=ax.errorbar(xaxis, data, yerr=STDV_data)

Here is my code:这是我的代码:

import pandas as pd 
import matplotlib.pyplot as plt
import numpy as np

#sketched code
df = pd.read_excel('Behavioraldata.xlsx')

plt.figure(figsize=(10, 7))
Day = df['Day']
Accuracy = df[['1', '2', '3', '4', '5', '6', '7', '8', '9']]
plt.plot(Day, Accuracy, alpha = 0.4)
Accuracy_mean = df[['1', '2', '3', '4', '5', '6', '7', '8', '9']].mean(axis=1)
plt.plot(Day, Accuracy_mean, color = "black", marker="s")
plt.axis([1, 22, 0.55, 1])
plt.axhline(y=0.8, color='black', linestyle='--', alpha=0.5)
plt.xlabel('Day')
plt.ylabel('Accuracy')
plt.title("Days to Acquisition by Rat")
ax = plt.subplot()
ax.set_xticks(Day)
plt.show()

I tried to format the code so it would fit with mine:我尝试格式化代码以使其适合我的:

stdv_accuracy_mean=np.std(accuracy_mean)

line_stdv=ax.errorbar(xaxis, accuracy_mean, yerr=stdv_accuracy_mean)

But to no avail.但无济于事。 Any help would be really appreciated.任何帮助将非常感激。

This is what my graph looks like so far:这是我的图表到目前为止的样子:1

And I would like for it to look like the graphs in these threads: 1 2我希望它看起来像这些线程中的图表: 1 2

From what you've written, I think the missing piece is pyplot.fill_between() :根据您所写的,我认为缺少的部分是pyplot.fill_between()

Faking up some data I get this伪造一些数据我得到了这个

from matplotlib import pyplot as plt
import numpy as np

# fake up some data
x = np.linspace(1, 22, 22)
y = np.linspace(.50, 1.0, 22)
errorbar = np.random.normal(.25, .1, size=y.shape)
y += np.random.normal(0, 0.1, size=y.shape)


plt.plot(x, y, 'k-')
plt.fill_between(x, y-errorbar, y+errorbar)
plt.show()

在此处输入图像描述

errorbar already draws the line for you, so you don't need two commands. errorbar已经为你画好了线,所以你不需要两个命令。 Instead, you can do something like this:相反,您可以执行以下操作:

fig, ax = plt.subplots(figsize=(15,10))

std_data = np.std(Accuracy_mean)
ax.errorbar(Day, Accuracy_mean, yerr=std_data, color='k', marker='s', ecolor='C0')

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM