Matplotlib Scatter图以numpy行索引作为标记

时间:2017-03-02 11:15:22

标签: python numpy matplotlib

我有一个numpy数组,我试图使用matplotlib用散点图绘制它。

var sum_pairs=function(ints, s, und){
    var h = {}
    for(var i=ints.length - 1; i >= 0; --i){
      h[ints[i]] = i
    }
    for(var i=0; i < ints.length; ++i){
      var ci = ints[i]
      var e = h[s - ci]
      if(e < i){
        return [s - ci, ci]
      }
    }
    return und
}

这给了我类似的东西: plot

现在我想用与numpy数组中行的索引相对应的数字替换红点。我怎么能这样做?

谢谢!

1 个答案:

答案 0 :(得分:7)

您可以使用plt.text

执行此操作
from matplotlib import pyplot as plt

import numpy as np
N = 100
matrix = np.random.rand(N,2)

plt.plot(matrix[:,0],matrix[:,1], 'ro', alpha = 0.5)
for i in range(matrix.shape[0]):
    plt.text(matrix[i,0], matrix[i,1], str(i))

plt.show()

enter image description here

如果您想替换红点,请设置alpha = 0.0

enter image description here