代码之家  ›  专栏  ›  技术社区  ›  Nick X Tsui

连接线如何连接点的惯例

  •  0
  • Nick X Tsui  · 技术社区  · 7 年前

    我有两个矩阵, x y X 有大小 10 行和 50 列,也是 Y

    我的数据是成对的。意思是

    x[0][:] <-> y[0][:]
    x[1][:] <-> y[1][:]
    x[2][:] <-> y[2][:]
    
    ......
    x[49][:] <-> y[0][:]
    

    当我使用以下命令进行绘图时,

    plot(x[:][:],y[:][:],'b-o')
    

    plot(x,y,'b-o')
    

    为了进行绘图,“-”在水平方向上连接点,如下所示:

    enter image description here

    但是,当我只绘制一行信号时:

    plot(x[0][:],y[0][:],'b-o')
    

    看起来是对的:

    enter image description here

    我希望“-”以水平方式连接点。像这样的:

    enter image description here

    不做for循环,我该怎么做矩阵格式?谢谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   ak_slick    7 年前

    制作一些数据来演示。

    import numpy as np
    from matplotlib import pyplot as plt
    
    x = np.matrix(
        [
            [1, 1, 1, 1],
            [2, 2, 2, 2],
            [3, 3, 3, 3],
            [4, 4, 4, 4]
        ]
    )
    
    y = x.transpose()
    
    # Vertical Lines of grid:
    plt.plot(x, y, 'b-o')
    plt.show()
    

    vert

    # Horizontal Lines
    plt.plot(x, y, 'b-o')
    plt.show()
    

    hori

    # Together (this is what I think you want)
    plt.plot(y, x, 'b-o')
    plt.plot(x, y, 'b-o')
    plt.show()
    

    grid

    如果你试着把它们连接起来在一个大矩阵中,它会做一些看似愚蠢的事情,把我们真正不想连接的几个点连接起来。

    # sillyness
    x1 = np.concatenate((x, y), axis=0)
    y1 = np.concatenate((y, x), axis=0)
    
    plt.plot(x1, y1, 'b-o')
    plt.show()
    

    enter image description here

    推荐文章