KGreen
KGreen

Reputation: 305

Add the y=x line in a scatter graph

I have a plot similar to the one below and I want to put the y=x line on the same plot. I want the line to be solid (unlike the below, which is a scatter plot), how do I do this? [This was written in python].

#Data: Example data
x_data = [24.48,24.65,19.14,23.61,22.96,24.48,24.73]
y_data = [24.50,24.50,19.15,23.58,22.93,24.48,24.73]
plt.scatter(x_data, y_data, color = 'green', marker = '+', label = 'Example data')
plt.title('Example Data Plotted')
plt.xlabel('X_data')
plt.ylabel('Y_data')
plt.legend()
plt.show()

Upvotes: 1

Views: 6862

Answers (2)

Mateo Torres
Mateo Torres

Reputation: 1625

You can add something like

plt.plot(x_data, x_data, color = 'red', label = 'x=y')

before plt.show()

like this:

#Data: Example data
x_data = [24.48,24.65,19.14,23.61,22.96,24.48,24.73]
y_data = [24.50,24.50,19.15,23.58,22.93,24.48,24.73]
plt.scatter(x_data, y_data, color = 'green', marker = '+', label = 'Example data')
plt.plot(x_data, x_data, color = 'red', label = 'x=y')
plt.title('Example Data Plotted')
plt.xlabel('X_data')
plt.ylabel('Y_data')
plt.legend()
plt.show()

which results in this: enter image description here

Upvotes: 1

jimsu2012
jimsu2012

Reputation: 170

Add

plt.xlim((0, 25)) # restricts x axis from 0 to 25
plt.ylim((0, 25)) # restricts x axis from 0 to 25
plt.plot([0, 25], [0, 25]) # plots line y = x

before plt.show()

Upvotes: 1

Related Questions