I was trying to plot two regression lines on the same plot using matplotlib and the plot returned duplicate legends for the line labeled as: 'OLS regression line'. I could not figure out why. Could someone explain the possible reasons?
fig, ax = plt.subplots(figsize =(10,5))
ax.scatter(x, y)
ax.set_ylabel('y', fontsize=12)
ax.set_xlabel('x', fontsize=12)
ax.plot(x2, y_hat, '-g', label='OLS regression line')
ax.plot(x, y_, '-r', label='population regression line')
#show plots
plt.legend(loc='upper left')
plt.show()
This is the plot: Output Plot
Thank you.