【问题标题】:scipy equivalent for MATLAB spyMATLAB spy 的 scipy 等效项
【发布时间】:2013-09-10 04:55:40
【问题描述】:

我一直在将 isomap 算法的代码从 MATLAB 移植到 Python。我正在尝试使用 spy 函数来可视化稀疏模式。

MATLAB 命令:

spy(sparse(A));
drawnow;

Python 命令:

matplotlib.pyplot.spy(scipy.sparse.csr_matrix(A))
plt.show()

我无法使用上述命令在 Python 中重现 MATLAB 结果。以非稀疏格式仅使用带有 A 的命令给出了与 MATLAB 非常相似的结果。但这需要很长时间(A 是 2000 到 2000 年)。 scipy 的稀疏函数的 MATLAB 等价物是什么?

【问题讨论】:

    标签: python matlab scipy


    【解决方案1】:

    我刚刚发布了betterspy,可以说它在这里做得更好。安装

    pip install betterspy
    

    并运行

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

    【讨论】:

    • 看起来是 matplotlib 的一个很好的补充。是否可以使用轴属性,例如ax.set_xticklabels(...) 用于betterspy.show()?顺便说一句,干得好。
    • 只需使用betterspy.plot(A),在gca() 上设置任何你想要的,然后pyplot.show() 图像。
    • 哦,博伊,就像一个魅力。在自述文件上没有看到该方法。我建议和改进文档。谢谢。 @Nitin 这应该是公认的答案。这里有一个简单的例子:import matplotlib.pyplot as plt, betterspy as bspy, scipy.sparse as sps; A=sps.rand(5,5, density=.4); bspy.plot(A); ax=plt.gca(); ax.set_xticklabels(['','a','b','c','d','e']); plt.show()
    【解决方案2】:

    也许是你的matplotlib 版本惹了麻烦,至于我的scipy.sparsematplotlib.pylab 配合得很好。

    请参阅下面生成附加的“间谍”图的示例代码。

    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__
    

    这给出了这个情节:

    【讨论】:

    • 谢谢。我发现错误出现在我在 Python 中创建 A 矩阵的代码中。一个快速问题。产生的蓝色方块是非零,对吗?
    【解决方案3】:

    使用较小的标记:

    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()
    

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 1970-01-01
      • 2018-04-26
      • 2015-04-18
      • 2019-01-09
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      相关资源
      最近更新 更多