如何为黄砖残留图设置颜色

时间:2019-10-10 17:04:34

标签: python matplotlib yellowbrick

我想使用yellowbrick Residual plot来显示线性回归模型的残差。从文档中,我可以看到Regression_Plot接受训练数据集的单一颜色值。

  

train_colorcolor,默认值:“ b”   训练数据的残差用这种颜色绘制,但不透明度为0.5,以确保
  测试数据残差更明显。可以是任何matplotlib颜色。

我想让各个散点的颜色与可比较的绘图相匹配,在该绘图中我将绘制回归点和数据点。

import numpy as np
from scipy.stats import linregress
import matplotlib.pyplot as plt

from yellowbrick.regressor import ResidualsPlot
from sklearn.linear_model import LinearRegression


data = np.array([[5.71032104e-01, 2.33781600e+03],
       [6.28682565e-01, 2.25247200e+03],
       [1.23262572e+00, 2.82244800e+03],
       [7.44029502e-01, 2.49936000e+03],
       [4.01478749e-01, 2.04825600e+03],
       [3.46455997e-01, 2.32867200e+03],
       [5.15778747e-01, 2.39268000e+03],
       [4.16115498e-01, 2.20218000e+03],
       [3.24103999e-01, 2.07264000e+03],
       [4.29520513e-01, 1.97815200e+03],
       [7.72794999e-01, 2.46278400e+03]])

x = data[:,1]
y = data[:,0]
names = np.array(['COTTONWOOD CREEK', 'EMIGRANT SUMMIT', 'GRAND TARGHEE',
       'PHILLIPS BENCH', 'PINE CREEK PASS', 'SALT RIVER SUMMIT',
       'SEDGWICK PEAK', 'SLUG CREEK DIVIDE', 'SOMSEN RANCH',
       'WILDHORSE DIVIDE', 'WILLOW CREEK'], dtype=object)
colors = ['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99']


slope, intercept, r_value, p_value, std_err = linregress(x ,y)
xHat = np.linspace(x.min()-300,x+300, 100 )
yHat = y * slope +intercept

colors = ['#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f','#ff7f00','#cab2d6','#6a3d9a','#ffff99']

fig,(ax,ax1) = plt.subplots(nrows=2)
for name, x_, y_, color in zip(names, x, y, colors):
    ax.scatter(x_, y_, label = name, c = color)
ax.plot(xHat, xHat*slope + intercept, 'k--', marker=None)
ax.set_xlim(x.min()-200,x.max()+200)
leg = ax.legend(fontsize='x-small', loc='lower right')
ax.text(1934,1.27,'y=' + str(np.round(slope,6))+'x'+ str(np.round(intercept, 3)))
ax.text(1934,1.1, 'R$^2$ =' + str(np.round(r_value**2,4)))


linreg = LinearRegression()
vizul  = ResidualsPlot(linreg, hist=False)
vizul.fit(x.reshape(-1,1) ,y.reshape(-1,1))
vizul.poof(ax=ax1)
plt.tight_layout()

enter image description here

是否有可能无需在残差图上使用基本的matplotlib来实现?

谢谢。

0 个答案:

没有答案