如果我在 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/