scipy equivalent for MATLAB spy

前端 未结 3 934
我寻月下人不归
我寻月下人不归 2020-12-15 09:35

I have been porting code for an isomap algorithm from MATLAB to Python. I am trying to visualize the sparsity pattern using the spy function.

MATLAB command:

相关标签:
3条回答
  • 2020-12-15 10:09

    Maybe it's your version of matplotlib that makes trouble, as for me scipy.sparse and matplotlib.pylab work well together.

    See sample code below that produces the 'spy' plot attached.

    import matplotlib.pylab as plt
    import scipy.sparse as sps
    A = sps.rand(10000,10000, density=0.00001)
    M = sps.csr_matrix(A)
    plt.spy(M)
    plt.show()
    
    # Returns here '1.3.0'
    matplotlib.__version__
    

    This gives this plot: enter image description here

    0 讨论(0)
  • 2020-12-15 10:09

    With smaller markers:

    import matplotlib.pylab as pl
    import scipy.sparse as sps
    import scipy.io
    import sys
    A=scipy.io.mmread(sys.argv[1])
    pl.spy(A,precision=0.01, markersize=1)
    pl.show()
    
    0 讨论(0)
  • 2020-12-15 10:12

    I just released betterspy, which arguably does a better job here. Install with

    pip install betterspy
    

    and run with

    import betterspy
    from scipy import sparse
    
    A = sparse.rand(20, 20, density=0.1)
    betterspy.show(A)
    betterspy.write_png("out.png", A)
    

    0 讨论(0)
提交回复
热议问题