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

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

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

最佳答案

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

这是一个基于您的示例:

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]]

关于python - 获取由 scatter() 创建的 PathCollection 中的点的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32213889/

相关文章:

python - matplotlib mySQL 空白图

python-2.7 - 导入 matplotlib._png 作为 _png 导入 : Error: DLL load failed: The specified module could not be found

python - 错误 : unable to connect to cassandra server. 未配置表

用于数字行和可选破折号+数字的 Python 正则表达式。为什么不匹配?

python - matplotlib 颜色图 : do not resize

python - 如何绘制渐近线?

python - 维恩3 : How to reposition circles and labels?

python - 将特征图(3D 数组)拆分为 2D 数组

python - Python Twisted中的互斥量

python - 如何在opencv Python中从图像中心(x,y)绘制正方形?