Reputation: 452
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)
Upvotes: 1
Views: 134
Reputation: 6407
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)
Upvotes: 1