Search code examples
pythonmatplotlibscikit-learnlinear-regression

AttributeError: 'Line3D' object has no attribute '_verts3d'


I've been trying to draw a regression line for a multivariable regression, both ENGINESIZE and FUELCONSUMPTION_CITY are independent variables and CO2EMISSIONis the dependent variable.

I was trying to draw a regression line but no matter what, I'm not able to draw it as it keeps showing me the same error.

Below is my code:-

z_cord = regr.coef_[0][0]*train_engine[['ENGINESIZE']]
z_cod =  regr.coef_[0][1]*train_engine[['FUELCONSUMPTION_CITY']]
s = y_cord.add(y_cod, fill_value=0)

l = []
for index, row in s.iterrows():
    l.append(row['ENGINESIZE']+row['FUELCONSUMPTION_CITY'] + regr.intercept_)
    
z =  pd.DataFrame(l,columns=['CO2EMISSION'])

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(data[['ENGINESIZE']],data[['FUELCONSUMPTION_CITY']],co2_data)

x = train_engine[['ENGINESIZE']]
y = train_engine[['FUELCONSUMPTION_CITY']]

ax.plot3D(x,y,z,color='red')

plt.show()

Every time i run it its giving me these errors

ValueError: input operand has more dimensions than allowed by the axis remapping

AttributeError: 'Line3D' object has no attribute '_verts3d'

the scatter plot is drawn when i comment ax.plot3D(x,y,z,color='red') line.

I don't know where I'm going wrong, some help would be appreciated.


Solution

  • In your code, x and y are two-dimensional. However, ax.plot3D requires x and y to be one-dimensional and you can make them one-dimensional by:

    x = train_engine['ENGINESIZE']
    y = train_engine['FUELCONSUMPTION_CITY']
    

    Note that we can get number of dimensions of x and y by ndim:

    x.ndim, y.ndim