Reputation: 87
Here is the code that I have for now, I have some data and I want the uncertainty bars to stay. What is the piece of scipy
that I need?
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import statistics
from statistics import stdev
y1 = [73.64, 76.47, 75.70, 71.71, 73.70, 79.39]
y2 = [219.70, 206.96, 235.31, 189.91, 256.48, 210.25]
y3 = [241.11, 271.70, 255.19, 229.67, 222.30, 200.70]
y4 = [256.59, 263.97, 262.17, 243.14, 245.42, 256.55]
y1_mean = statistics.mean(y1)
y2_mean = statistics.mean(y2)
y3_mean = statistics.mean(y3)
y4_mean = statistics.mean(y4)
y = np.array([y1_mean, y2_mean, y3_mean, y4_mean])
x = np.array([0,0.3,1.5,3])
e = np.array([stdev(y1), stdev(y2), stdev(y3), stdev(y4)])
plt.errorbar(x, y, e, linestyle = 'none', color = 'turquoise' )
plt.scatter(x, y, color = 'green')
plt.xlabel('x-coordinates')
plt.ylabel('y-coordinates')
plt.title('Sample graph')
plt.show()
I wanted it to be like that, but fitted for my data:
Upvotes: 3
Views: 929
Reputation: 1285
Not quite sure what you want because of poor explanation, but I will try to help you by using plt.semilogy()
and curve fit. You can try with plt.semilogy(x,y)
and see what you get, but in this solution i wanted to fit curve, so here is the edited code, hope it will help you or guide you through your problem:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import statistics as st
import scipy as sp
from scipy.optimize import curve_fit
y1 = [6,5,6,7,8]
y2 = [8,9,10,10,11]
y3 = [14,15,16,14,14]
y4 = [16,17,18,19,18]
y1_mean = statistics.mean(y1)
y2_mean = statistics.mean(y2)
y3_mean = statistics.mean(y3)
y4_mean = statistics.mean(y4)
y = np.array([y1_mean, y2_mean, y3_mean, y4_mean])
x = np.array([3,5,6,8])
e = np.array([st.stdev(y1), st.stdev(y2), st.stdev(y3), st.stdev(y4)])
def f(x,a,b,c):
return a*(np.square(x))+(b*x)+c
popt, pcov = curve_fit(f, x, y)
fitA = popt[0]
fitB = popt[1]
fitC = popt[0]
yFit = f(x,fitA,fitB,fitC)
plt.errorbar(x, y, e, linestyle = 'none', color = 'k', label="Points" )
plt.semilogy(x,y, color='g', linestyle="--", label="Log")
plt.semilogy(x,yFit,color='r', label="Log Fit")
plt.scatter(x, y, color = 'k')
plt.xlabel('x-coordinates')
plt.ylabel('y-coordinates')
plt.title('Sample graph')
plt.legend()
plt.show()
And this is what i get
Upvotes: 1