如何使用plt.gca()从matplotlib散点图获取x和y坐标? [英] How to get x and y coordinates from matplotlib scatter plot using plt.gca()?
本文介绍了如何使用plt.gca()从matplotlib散点图获取x和y坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从 Matplotlib Axes 对象中获取散点图点的 x 和 y 坐标?对于plt.plot()
,有一个名为data
的属性,但是下面的代码不起作用:
Is there a way to get the x and y coordinates of scatter plot points from a Matplotlib Axes object? For plt.plot()
, there is an attribute called data
, but the following code does not work:
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]]
这篇关于如何使用plt.gca()从matplotlib散点图获取x和y坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文