如果我在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()
Run Code Online (Sandbox Code Playgroud)
其中列出了集合的所有属性,但我不认为它们中的任何一个是坐标
您可以通过首先将偏移设置为数据坐标然后返回偏移来从散点图获取点的位置,即您绘制的原始数据.
以下是基于您的示例:
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()
Run Code Online (Sandbox Code Playgroud)
打印出来:
[[0 3]
[1 2]
[2 1]
[3 0]]
Run Code Online (Sandbox Code Playgroud)
小智 7
由于set_offset_position在Matplotlib 3.3(当前版本)中已弃用,这里是另一种方法:
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]
points = ax.scatter(x, y)
print(points.get_offsets().data)
Run Code Online (Sandbox Code Playgroud)
打印出:
[[0. 3.]
[1. 2.]
[2. 1.]
[3. 0.]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2275 次 |
| 最近记录: |