import os
from PIL import Image as PImage
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from scipy.stats import chisquare
# Read in csv file
# File: https://github.com/mGalarnyk/Python_Tutorials/blob/master/Python_Basics/Linear_Regression/linear.csv
raw_data = pd.read_csv(r"C:\Users\Aidan\Desktop\NEW TASK\Amos_2001_4p2_APD_CONC_Fig2C_OC.csv")
# Removes rows with NaN in them
filtered_data = raw_data[~np.isnan(raw_data["y"])]
x_y = np.array(filtered_data)
x, y, y_err = x_y[:,0], x_y[:,1], x_y[:,2]
# Reshaping
x, y = x.reshape(-1,1), y.reshape(-1, 1)
# Linear Regression Object
lin_regression = LinearRegression()
# Fitting linear model to the data
lin_regression.fit(x,y)
# Get slope of fitted line
m = lin_regression.coef_
# Get y-Intercept of the Line
b = lin_regression.intercept_
# Get Predictions for original x values
# you can also get predictions for new data
predictions = lin_regression.predict(x)
chi= chisquare(predictions, y)
# following slope intercept form
print ("formula: y = {0}x + {1}".format(m, b))
print(chi)
# Plot the Original Model (Black) and Predictions (Blue)
plt.scatter(x, y, color='black')
plt.plot(x, predictions, color='blue',linewidth=3)
plt.errorbar(x, y, yerr=y_err, fmt='o', capsize=4, color='black')
plt.show()
导入的csv数据:
1.01214,0.3609367,-0.01214
1.992202,0.341559,0.007798
2.995016,0.3510846,0.004984
3.974359,0.3405953,0.025641
4.976273,0.3612314,0.023727
5.954718,0.3618527,0.045282
6.984058,0.3536173,0.015942
7.962502,0.3542386,0.037498
8.967653,0.3348767,0.032347
9.969748,0.3532908,0.030252
错误:
runfile('C:/Users/Aidan/.spyder-py3/temp.py', wdir ='C:/Users/Aidan/.spyder-py3')追溯(最近一次通话结束):
文件“”,第1行,在 运行文件('C:/Users/Aidan/.spyder-py3/temp.py',wdir ='C:/Users/Aidan/.spyder-py3')
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”, 运行文件中的第705行 execfile(文件名,命名空间)
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”, 第102行,在execfile中 exec(compile(f.read(),文件名,'exec'),命名空间)
文件“ C:/Users/Aidan/.spyder-py3/temp.py”,第15行,在 filtered_data = raw_data [〜np.isnan(raw_data [“ y”])]
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ pandas \ core \ frame.py”, 第2685行,在 getitem 返回self._getitem_column(key)
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ pandas \ core \ frame.py”, _getitem_column中的第2692行 返回self._get_item_cache(key)
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ pandas \ core \ generic.py”, 第2486行,在_get_item_cache中 值= self._data.get(item)
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ pandas \ core \ internals.py”, 4115行,进入 loc = self.items.get_loc(item)
文件 “ C:\ Users \ Aidan \ Anaconda3 \ lib \ site-packages \ pandas \ core \ indexes \ base.py”, 第3065行,位于get_loc中 返回self._engine.get_loc(self._maybe_cast_indexer(key))
文件“ pandas_libs \ index.pyx”,第140行,在 pandas._libs.index.IndexEngine.get_loc
文件“ pandas_libs \ index.pyx”,第162行,在 pandas._libs.index.IndexEngine.get_loc
文件“ pandas_libs \ hashtable_class_helper.pxi”,行1492,在 pandas._libs.hashtable.PyObjectHashTable.get_item
文件“ pandas_libs \ hashtable_class_helper.pxi”,行1500,在 pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError:'y'
因此,在CSV中没有第三列的情况下,此脚本可以完美执行。我想在错误栏行中包含数据的第三列。如何在脚本中实现err bar?
答案 0 :(得分:1)
只需将错误栏保存在变量中,如下所示:
let MQ = MathQuill.getInterface(MathQuill.getInterface.MAX);
并将x, y, y_err = x_y[:,0], x_y[:,1], x_y[:,2]
用作
plt.errorbar
具有以下输出。您可以通过以下页面的更多参数自定义plt.errorbar(x, y, yerr=y_err, fmt='o', capsize=4, color='black')
:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.errorbar.html