gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:10:05 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com