我有这些数据:
new.csv:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model, cross_validation
import random
data = pd.read_csv('./data/new.csv', names=['X', 'Y'], header=0)
fig = plt.figure()
ax = plt.axes()
x = data.loc[:,'X'].to_frame()
y = data.loc[:,'Y'].to_frame()
x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.3, random_state=0)
regr = linear_model.LinearRegression()
regr.fit(x_train, y_train)
ax.set(xlabel='X', ylabel='Y', title='X vs Y')
ax.scatter(x_test,y_test, alpha=0.5, cmap='viridis')
ax.plot(x_test, regr.predict(x_test), color='red', linewidth=2)
我试图应用线性回归模型。
plt.errorbar(x_test,y_test, yerr=1, fmt='o');
直到这里,一切都运行良好。我尝试添加错误栏的那一刻:
TypeError Traceback (most recent call last)
<ipython-input-29-cf35bd0c650f> in <module>()
29
30 #dy=1
---> 31 plt.errorbar(x_test,y_test, yerr=1);
../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/pyplot.py in errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, hold, data, **kwargs)
2835 xlolims=xlolims, xuplims=xuplims,
2836 errorevery=errorevery, capthick=capthick, data=data,
-> 2837 **kwargs)
2838 finally:
2839 ax.hold(washold)
../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1817 warnings.warn(msg % (label_namer, func.__name__),
1818 RuntimeWarning, stacklevel=2)
-> 1819 return func(ax, *args, **kwargs)
1820 pre_doc = inner.__doc__
1821 if pre_doc is None:
../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs)
2924
2925 if yerr is not None:
-> 2926 lower, upper = extract_err(yerr, y)
2927 # select points without upper/lower limits in y and
2928 # draw normal errorbars for these points
../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in extract_err(err, data)
2873 # using list comps rather than arrays to preserve units
2874 low = [thisx - thiserr for (thisx, thiserr)
-> 2875 in cbook.safezip(data, err)]
2876 high = [thisx + thiserr for (thisx, thiserr)
2877 in cbook.safezip(data, err)]
../anaconda2/envs/python3/lib/python3.5/site-packages/matplotlib/axes/_axes.py in <listcomp>(.0)
2872 "dimensions as x, or 2xN.")
2873 # using list comps rather than arrays to preserve units
-> 2874 low = [thisx - thiserr for (thisx, thiserr)
2875 in cbook.safezip(data, err)]
2876 high = [thisx + thiserr for (thisx, thiserr)
TypeError: unsupported operand type(s) for -: 'str' and 'int'
我收到了标题中的错误。
完整错误是:
widget
答案 0 :(得分:0)
我试过了:
plt.errorbar(x_test.values, y_test.values, yerr=1, fmt='o');
它有效!