1

I have followed the suggestions from another threat, but this code still gives me the list object has no no attribute error - what is the correction?

import numpy as np 
import seaborn as sns
import matplotlib.pyplot as plt


#create data
x = np.array([4.1,7.1,5.2,7.1,0.3, 8.3, 9.7, 8.2])
y = np.array([0.7,0.72,0.5,0.73, 0.65, 0.57, 0.7, 0.8])

#create basic scatterplot
plt.plot(x, y, 'o',markersize=10,c='black')

#obtain m (slope) and b(intercept) of linear regression line
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatterplot 
fig = plt.plot(x, m*x+b)
ax = fig.add_subplot(111)
ax.set_ylim([0.45,0.8])

sns.despine(top=True, right=True, left=False, bottom=False)
plt.xlabel('Accuracy', fontsize=22)
plt.ylabel('Score', fontsize=22)
2
  • The documentation states that plt.plot(...) returns a list of Line2D objects. Commented Jan 30, 2023 at 17:15
  • Suggest a basic tutorial on how Matplotlib works. Matplotlib even has one, but the internet abounds: matplotlib.org/stable/tutorials/introductory/… Commented Jan 30, 2023 at 18:26

1 Answer 1

1

On the line

fig = plt.plot(x, m*x+b)

the plt.plot function returns a list of Line2d objects rather than a Figure object. I'd suggest doing the following:

fig, ax = plt.subplots()  # create figure and axes

# plot on ax (Axes) object
ax.plot(x, y, 'o',markersize=10,c='black')

#obtain m (slope) and b(intercept) of linear regression line
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatterplot 
ax.plot(x, m*x+b)
ax.set_ylim([0.45,0.8])

sns.despine(top=True, right=True, left=False, bottom=False)
ax.set_xlabel('Accuracy', fontsize=22)
ax.set_ylabel('Score', fontsize=22)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.