简体   繁体   English

使用python的matplotlib向散点图添加线

[英]Adding line to scatter plot using python's matplotlib

I am using python's matplotlib and want to create a matplotlib.scatter() with additional line.我正在使用 python 的matplotlib并想创建一个带有附加行的matplotlib.scatter() The line should proceed from the lower left corner to the upper right corner independent of the scatters content.这条线应该从左下角到右上角,与分散的内容无关。 A linear regression through the data, like in this post , is not what I am looking for.数据的线性回归,就像在这篇文章中一样,不是我想要的。 Also it should be dynamically and independent of the scatter input.它也应该是动态的并且独立于分散输入。

This should be the final plot:这应该是最后的情节:

在此处输入图片说明

EDIT:编辑:

Doing this got me the result:这样做让我得到了结果:

# Scatter Plot
x = data_calc_hourly.temp
y =  data_obs_hourly.temp

lineStart = data_calc_hourly.temp.min() 
lineEnd = data_calc_hourly.temp.max()  

plt.figure()
plt.scatter(x, y, color = 'k', alpha=0.5)
plt.plot([lineStart, lineEnd], [lineStart, lineEnd], 'k-', color = 'r')
plt.xlim(lineStart, lineEnd)
plt.ylim(lineStart, lineEnd)
plt.show()

Is there any better way ?有没有更好的方法?

This draws a diagonal line which is independent of the scatter plot data and which stays rooted to the axes even if you resize the window:这将绘制一条与散点图数据无关的对角线,即使您调整窗口大小,该对角线仍以轴为根:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.transforms as mtransforms

x, y = np.random.random((2, 100))*2
fig, ax = plt.subplots()
ax.scatter(x, y, c='black')
line = mlines.Line2D([0, 1], [0, 1], color='red')
transform = ax.transAxes
line.set_transform(transform)
ax.add_line(line)
plt.show()

在此处输入图片说明

Besides unutbu's answer one other option is to get the limits of the axis after you ploted the data and to use them to add the line.除了 unutbu 的答案之外,另一种选择是在绘制数据后获取轴的限制并使用它们添加线。 After this you will still need to change back the axis limits as they would change with the addition of the line:在此之后,您仍然需要更改轴限制,因为它们会随着行的添加而改变:

# Scatter Plot
x = data_calc_hourly.temp
y =  data_obs_hourly.temp

lineStart = data_calc_hourly.temp.min() 
lineEnd = data_calc_hourly.temp.max()  

plt.figure()
plt.scatter(x, y, color = 'k', alpha=0.5)
y_lim = plt.ylim()
x_lim = plt.xlim()
plt.plot(x_lim, y_lim, 'k-', color = 'r')
plt.ylim(y_lim)
plt.xlim(x_lim)
plt.show()

I have tried updating the min and max limits for the cases where X and Y axis have different max and min data.我尝试更新 X 轴和 Y 轴具有不同最大值和最小值数据的情况下的最小值和最大值限制。

x = data_calc_hourly.temp
y =  data_obs_hourly.temp

calc_min = data_calc_hourly.temp.min()
calc_max = data_calc_hourly.temp.max()

obs_min = data_obs_hourly.temp.min()
obs_max = data_obs_hourly.temp.max()

lineStart = min(calc_min,obs_min)
lineEnd = max(calc_max,obs_max)

plt.figure()
plt.scatter(x, y, color = 'k', alpha=0.5)
plt.plot([lineStart, lineEnd], [lineStart, lineEnd], color = 'r')
plt.xlim(lineStart, lineEnd)
plt.ylim(lineStart, lineEnd)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM