Python 如何在散点图中画线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12981696/
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
How to draw line inside a scatter plot
提问by ruffy
I can't believe that this is so complicated but I tried and googled for a while now.
我不敢相信这如此复杂,但我已经尝试并搜索了一段时间。
I just want to analyse my scatter plot with a few graphical features. For starters, I want to add simply a line.
我只想用一些图形特征来分析我的散点图。首先,我想简单地添加一行。
So, I have a few (4) points and I want to add a line to it, like in this plot (source: http://en.wikipedia.org/wiki/File:ROC_space-2.png)
所以,我有几 (4) 个点,我想给它添加一条线,就像这个图一样(来源:http: //en.wikipedia.org/wiki/File: ROC_space-2.png)


Now, this won't work. And frankly, the documentation-examples-gallery combo and content of matplotlib is a bad source for information.
现在,这行不通。坦率地说,matplotlib 的文档-示例-画廊组合和内容是一个糟糕的信息来源。
My code is based upon a simple scatter plot from the gallery:
我的代码基于图库中的简单散点图:
# definitions for the axes
left, width = 0.1, 0.85 #0.65
bottom, height = 0.1, 0.85 #0.65
bottom_h = left_h = left+width+0.02
rect_scatter = [left, bottom, width, height]
# start with a rectangular Figure
fig = plt.figure(1, figsize=(8,8))
axScatter = plt.axes(rect_scatter)
# the scatter plot:
p1 = axScatter.scatter(x[0], y[0], c='blue', s = 70)
p2 = axScatter.scatter(x[1], y[1], c='green', s = 70)
p3 = axScatter.scatter(x[2], y[2], c='red', s = 70)
p4 = axScatter.scatter(x[3], y[3], c='yellow', s = 70)
p5 = axScatter.plot([1,2,3], "r--")
plt.legend([p1, p2, p3, p4, p5], [names[0], names[1], names[2], names[3], "Random guess"], loc = 2)
# now determine nice limits by hand:
binwidth = 0.25
xymax = np.max( [np.max(np.fabs(x)), np.max(np.fabs(y))] )
lim = ( int(xymax/binwidth) + 1) * binwidth
axScatter.set_xlim( (-lim, lim) )
axScatter.set_ylim( (-lim, lim) )
xText = axScatter.set_xlabel('FPR / Specificity')
yText = axScatter.set_ylabel('TPR / Sensitivity')
bins = np.arange(-lim, lim + binwidth, binwidth)
plt.show()
Everything works, except the p5 which is a line.
一切正常,除了 p5 是一条线。
Now how is this supposed to work? What's good practice here?
现在这应该如何工作?这里有什么好的做法?
采纳答案by bmu
plottakes either y values and uses x as index array 0..N-1or x and y values as described in the documentation. So you could use
plot采用 y 值并使用 x 作为索引数组0..N-1或 x 和 y 值,如文档中所述。所以你可以使用
p5 = axScatter.plot((0, 1), "r--")
in your code to plot the line.
在您的代码中绘制线条。
However, you are asking for "good practice". The following code (hopefully) shows some "good practise" and some of the capabilities of matplotlib to create the plot you mention in your question.
但是,您要求的是“良好做法”。下面的代码(希望如此)显示了一些“良好的实践”和 matplotlib 的一些功能来创建您在问题中提到的情节。
import numpy as np
import matplotlib.pyplot as plt
# create some data
xy = np.random.rand(4, 2)
xy_line = (0, 1)
# set up figure and ax
fig, ax = plt.subplots(figsize=(8,8))
# create the scatter plots
ax.scatter(xy[:, 0], xy[:, 1], c='blue')
for point, name in zip(xy, 'ABCD'):
ax.annotate(name, xy=point, xytext=(0, -10), textcoords='offset points',
color='blue', ha='center', va='center')
ax.scatter([0], [1], c='black', s=60)
ax.annotate('Perfect Classification', xy=(0, 1), xytext=(0.1, 0.9),
arrowprops=dict(arrowstyle='->'))
# create the line
ax.plot(xy_line, 'r--', label='Random guess')
ax.annotate('Better', xy=(0.3, 0.3), xytext=(0.2, 0.4),
arrowprops=dict(arrowstyle='<-'), ha='center', va='center')
ax.annotate('Worse', xy=(0.3, 0.3), xytext=(0.4, 0.2),
arrowprops=dict(arrowstyle='<-'), ha='center', va='center')
# add labels, legend and make it nicer
ax.set_xlabel('FPR or (1 - specificity)')
ax.set_ylabel('TPR or sensitivity')
ax.set_title('ROC Space')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.legend()
plt.tight_layout()
plt.savefig('scatter_line.png', dpi=80)


By the way: I think that matplotlibs documentation is quite useful nowadays.
顺便说一句:我认为 matplotlibs 文档现在非常有用。
回答by Anake
the p5 line should be:
p5 行应该是:
p5 = axScatter.plot([1,2,3],[1,2,3], "r--")
argument 1 is a list of the x values, and argument 2 is a list of y values
参数 1 是 x 值的列表,参数 2 是 y 值的列表
If you just want a straight line, you only need to provide values for the extremities of the line.
如果你只想要一条直线,你只需要为线的末端提供值。

