Python 如果我没有跟踪进入的所有数据点,将 y=x 添加到 matplotlib 散点图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25497402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding y=x to a matplotlib scatter plot if I haven't kept track of all the data points that went in
提问by kuzzooroo
Here's some code that does scatter plot of a number of different series using matplotlib and then adds the line y=x:
这是一些使用 matplotlib 绘制多个不同系列的散点图的代码,然后添加行 y=x:
import numpy as np, matplotlib.pyplot as plt, matplotlib.cm as cm, pylab
nseries = 10
colors = cm.rainbow(np.linspace(0, 1, nseries))
all_x = []
all_y = []
for i in range(nseries):
x = np.random.random(12)+i/10.0
y = np.random.random(12)+i/5.0
plt.scatter(x, y, color=colors[i])
all_x.extend(x)
all_y.extend(y)
# Could I somehow do the next part (add identity_line) if I haven't been keeping track of all the x and y values I've seen?
identity_line = np.linspace(max(min(all_x), min(all_y)),
min(max(all_x), max(all_y)))
plt.plot(identity_line, identity_line, color="black", linestyle="dashed", linewidth=3.0)
plt.show()
In order to achieve this I've had to keep track of all the x and y values that went into the scatter plot so that I know where identity_lineshould start and end. Is there a way I can get y=x to show up even if I don't have a list of all the points that I plotted? I would think that something in matplotlib can give me a list of all the points after the fact, but I haven't been able to figure out how to get that list.
为了实现这一点,我必须跟踪进入散点图的所有 x 和 y 值,以便我知道identity_line应该从哪里开始和结束。有没有办法让 y=x 显示出来,即使我没有我绘制的所有点的列表?我认为 matplotlib 中的某些内容可以在事后为我提供所有要点的列表,但我无法弄清楚如何获取该列表。
采纳答案by Paul H
You don't need to know anything about your data per se. You can get away with what your matplotlib Axes object will tell you about the data.
您不需要了解有关数据本身的任何信息。您可以摆脱 matplotlib Axes 对象将告诉您的有关数据的信息。
See below:
见下文:
import numpy as np
import matplotlib.pyplot as plt
# random data
N = 37
x = np.random.normal(loc=3.5, scale=1.25, size=N)
y = np.random.normal(loc=3.4, scale=1.5, size=N)
c = x**2 + y**2
# now sort it just to make it look like it's related
x.sort()
y.sort()
fig, ax = plt.subplots()
ax.scatter(x, y, s=25, c=c, cmap=plt.cm.coolwarm, zorder=10)
Here's the good part:
这是好的部分:
lims = [
np.min([ax.get_xlim(), ax.get_ylim()]), # min of both axes
np.max([ax.get_xlim(), ax.get_ylim()]), # max of both axes
]
# now plot both limits against eachother
ax.plot(lims, lims, 'k-', alpha=0.75, zorder=0)
ax.set_aspect('equal')
ax.set_xlim(lims)
ax.set_ylim(lims)
fig.savefig('/Users/paul/Desktop/so.png', dpi=300)
Et voilà
等等


回答by Edward
In one line:
在一行中:
ax.plot([0,1],[0,1], transform=ax.transAxes)
ax.plot([0,1],[0,1], transform=ax.transAxes)
No need to modify the xlim or ylim.
无需修改 xlim 或 ylim。
回答by kilodalton
If you set scalex and scaley to False, it saves a bit of bookkeeping. This is what I have been using lately to overlay y=x:
如果将 scalex 和 scaley 设置为 False,则可以节省一些簿记。这是我最近用来叠加 y=x 的方法:
xpoints = ypoints = plt.xlim()
plt.plot(xpoints, ypoints, linestyle='--', color='k', lw=3, scalex=False, scaley=False)
or if you've got an axis:
或者如果你有一个轴:
xpoints = ypoints = ax.get_xlim()
ax.plot(xpoints, ypoints, linestyle='--', color='k', lw=3, scalex=False, scaley=False)
Of course, this won't give you a square aspect ratio. If you care about that, go with Paul H's solution.
当然,这不会给你一个方形的纵横比。如果您关心这个,请使用 Paul H 的解决方案。

