Python 获取由scatter()创建的PathCollection中点的位置

Python 获取由scatter()创建的PathCollection中点的位置,python,matplotlib,Python,Matplotlib,如果我在matplotlib中创建散点图,那么之后如何获取(或设置)点的坐标?我可以访问集合的一些属性,但我不知道如何获取点本身的坐标 我可以走到尽可能远的地方 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() 它列出了集合的所有属

如果我在matplotlib中创建散点图,那么之后如何获取(或设置)点的坐标?我可以访问集合的一些属性,但我不知道如何获取点本身的坐标

我可以走到尽可能远的地方

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

它列出了集合的所有属性,但我不认为它们中的任何一个是坐标

通过首先将偏移设置为数据坐标,然后返回偏移,可以从散点图中获取点的位置,即绘制的原始数据

以下是一个基于您的示例:

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]]
[[0. 3.]
 [1. 2.]
 [2. 1.]
 [3. 0.]]

由于Matplotlib 3.3(当前版本)中不推荐使用
设置偏移位置
,以下是另一种方法:

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]

points = ax.scatter(x, y)
print(points.get_offsets().data)
打印出:

[[0 3]
 [1 2]
 [2 1]
 [3 0]]
[[0. 3.]
 [1. 2.]
 [2. 1.]
 [3. 0.]]