Plotting lines connecting points

前端 未结 4 1735
挽巷
挽巷 2020-12-03 02:32

I know there is another very similar question, but I could not extract the information I need from it.

plotting lines in pairs

I have 4 points in the (

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 03:37

    I realize this question was asked and answered a long time ago, but the answers don't give what I feel is the simplest solution. It's almost always a good idea to avoid loops whenever possible, and matplotlib's plot is capable of plotting multiple lines with one command. If x and y are arrays, then plot draws one line for every column.

    In your case, you can do the following:

    x=np.array([-1 ,0.5 ,1,-0.5])
    xx = np.vstack([x[[0,2]],x[[1,3]]])
    y=np.array([ 0.5,  1, -0.5, -1])
    yy = np.vstack([y[[0,2]],y[[1,3]]])
    plt.plot(xx,yy, '-o')
    

    Have a long list of x's and y's, and want to connect adjacent pairs?

    xx = np.vstack([x[0::2],x[1::2]])
    yy = np.vstack([y[0::2],y[1::2]])
    

    Want a specified (different) color for the dots and the lines?

    plt.plot(xx,yy, '-ok', mfc='C1', mec='C1')
    

提交回复
热议问题