Reputation: 31
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. 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).
For further clarification: there are nine total subjects and each of them have an accuracy that ranges from ~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, i.e., 50% on day 1, 65% day 2, etc).
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:
And I would like for it to look like the graphs in these threads: 1 2
Upvotes: 0
Views: 3392
Reputation: 3549
From what you've written, I think the missing piece is 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()
Upvotes: 1
Reputation: 150735
errorbar
already draws the line for you, so you don't need two commands. 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:
Upvotes: 0