gpt4 book ai didi

python - 如何使用 plt.gca() 从 matplotlib 散点图中获取 x 和 y 坐标?

转载 作者:太空宇宙 更新时间:2023-11-04 05:26:12 25 4
gpt4 key购买 nike

有没有办法从 Matplotlib Axes 对象获取散点图点的 x 和 y 坐标?对于plt.plot(),有一个名为data的属性,但是下面的代码不起作用:

x = [1, 2, 6, 3, 11]
y = [2, 4, 10, 3, 2]
plt.scatter(x, y)
print(plt.gca().data)
plt.show()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-30-9346ca31279c> in <module>()
41 y = [2, 4, 10, 3, 2]
42 plt.scatter(x, y)
---> 43 print(plt.gca().data)
44 plt.show()

AttributeError: 'AxesSubplot' object has no attribute 'data'

最佳答案

import matplotlib.pylab as plt

x = [1, 2, 6, 3, 11]
y = [2, 4, 10, 3, 2]
plt.scatter(x, y)
ax = plt.gca()
cs = ax.collections[0]
cs.set_offset_position('data')
print cs.get_offsets()

输出是

[[ 1  2]
[ 2 4]
[ 6 10]
[ 3 3]
[11 2]]

关于python - 如何使用 plt.gca() 从 matplotlib 散点图中获取 x 和 y 坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38578873/

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