Home > OS >  Getting AttributeError: 'Line2D' object has no property 'colour' for a simple li
Getting AttributeError: 'Line2D' object has no property 'colour' for a simple li

Time:05-01

I have written a basic linear plot code as below:

import matplotlib.pyplot as plt
a = [1, 2, 3, 4, 5]
b = [2, 3, 4, 5, 6]
plt.scatter(a, b)
plt.plot(a, b, colour = 'black')

But for this I am getting - AttributeError: 'Line2D' object has no property 'colour'

However, when I ran the following code, copied from sklearn, I did not get any error:

# Code source: Jaques Grobler
# License: BSD 3 clause

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score

# Load the diabetes dataset
diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)

# Use only one feature
diabetes_X = diabetes_X[:, np.newaxis, 2]

# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]

# Split the targets into training/testing sets
diabetes_y_train = diabetes_y[:-20]
diabetes_y_test = diabetes_y[-20:]

# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)

# Make predictions using the testing set
diabetes_y_pred = regr.predict(diabetes_X_test)

# The coefficients
print("Coefficients: \n", regr.coef_)
# The mean squared error
print("Mean squared error: %.2f" % mean_squared_error(diabetes_y_test, diabetes_y_pred))
# The coefficient of determination: 1 is perfect prediction
print("Coefficient of determination: %.2f" % r2_score(diabetes_y_test, diabetes_y_pred))

# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test, color="black")
plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

The scatter plot was in blue colour and the plotted line was in black colour. Is there anything I m missing?

CodePudding user response:

method plot param is color,not colour

import matplotlib.pyplot as plt
a = [1, 2, 3, 4, 5]
b = [2, 3, 4, 5, 6]
plt.scatter(a, b)
plt.plot(a, b, color = 'black')
plt.show()

CodePudding user response:

Yeah this just a french typo mistake :

What you written you go : plt.plot(a, b, colour = 'black')

What you want to written you go : plt.plot(a, b, color = 'black')

  • Related