【问题标题】:plotting coordinate as a matrix matplotlib python将坐标绘制为矩阵 matplotlib python
【发布时间】:2013-02-13 12:30:11
【问题描述】:

我有一组坐标,比如[(2,3),(45,4),(3,65)] 我需要将它们绘制为矩阵无论如何我可以在 matplotlib 中做到这一点,所以我希望它有这种外观http://imgur.com/Q6LLhmk

【问题讨论】:

    标签: python matplotlib wxpython


    【解决方案1】:

    编辑:我的原始答案使用了ax.scatter。这有一个问题:如果两个点并排,ax.scatter 可能会在它们之间画出一点空间,具体取决于比例:

    例如,用

    data = np.array([(2,3),(3,3)])
    

    这是放大的细节:

    所以这里有一个解决这个问题的替代解决方案:

    import matplotlib.pyplot as plt
    import numpy as np
    
    data = np.array([(2,3),(3,3),(45,4),(3,65)])
    N = data.max() + 5
    
    # color the background white (1 is white)
    arr = np.ones((N,N), dtype = 'bool')
    # color the dots black (0)
    arr[data[:,1], data[:,0]] = 0
    
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    
    ax.imshow(arr, interpolation='nearest', cmap = 'gray')
    ax.invert_yaxis()
    # ax.axis('off')
    plt.show()
    

    无论您放大多少,(2,3) 和 (3,3) 处的相邻方块都将保持并排。

    不幸的是,与ax.scatter 不同,使用ax.imshow 需要构建一个N x N 数组,因此它可能比使用ax.scatter 更占用内存。但是,除非data 包含非常大的数字,否则这应该不是问题。

    【讨论】:

    • 您先生是个传奇。非常感谢您
    猜你喜欢
    • 1970-01-01
    • 2015-09-21
    • 2014-01-31
    • 2014-05-22
    • 1970-01-01
    • 2017-03-20
    • 2020-03-31
    • 2011-03-03
    • 2014-02-26
    相关资源
    最近更新 更多