在散点图中添加y = x线

时间:2020-01-19 22:44:39

标签: python matplotlib plot scatter

我有一个与下面的图相似的图,并且我想将y = x线放在同一图上。我希望线是实线(与下面的散点图不同),该怎么做? [这是用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()

2 个答案:

答案 0 :(得分:0)

添加

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

plt.show()之前

答案 1 :(得分:0)

您可以添加

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

plt.show()之前

像这样:

#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()

导致以下结果: enter image description here

相关问题