获取由scatter()创建的PathCollection中的点的位置 [英] Get positions of points in PathCollection created by scatter()
问题描述
如果我在 matplotlib 中创建一个散点图,那么我之后如何获取(或设置)点的坐标?我可以访问集合的一些属性,但我无法找到如何获取点本身的坐标.
If I create a scatterplot in matplotlib, how do I then get (or set) the coordinates of the points afterwards? I can access some properties of the collection, but I can't find out how to get the coordinates of the points themselves.
我可以做到;
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = [0,1,2,3]
y = [3,2,1,0]
ax.scatter(x,y)
ax.collections[0].properties()
列出了集合的所有属性,但我不认为其中任何一个都是坐标
Which lists off all the properties of the collection, but I don't recognize any of them as being the coordinates
推荐答案
您可以通过首先将偏移量设置为数据坐标,然后返回散点图中的点的位置,即您绘制的原始数据抵消.
You can get the locations of the points, that is the original data you plotted, from a scatter plot by first setting the offsets to data coordinates and then returning the offsets.
以下是基于您的示例:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
x = [0,1,2,3]
y = [3,2,1,0]
ax.scatter(x,y)
d = ax.collections[0]
d.set_offset_position('data')
print d.get_offsets()
打印出来的:
[[0 3]
[1 2]
[2 1]
[3 0]]
这篇关于获取由scatter()创建的PathCollection中的点的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!