Dashed lines from points to axes in matplotlib

后端 未结 2 1087
再見小時候
再見小時候 2020-12-29 14:13

I have a set of points which I am plotting like this:

import matplotlib.pyplot as plt`

x = [1,2,3,4,5,6]
y = [1,4,9,16,25,36]

plt.scatter(x, y)

plt.show()         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 14:36

    Using hlines and vlines you can plot horizontal and vertical lines respectively.

    import matplotlib.pyplot as plt
    
    x = [1,2,3,4,5,6]
    y = [1,4,9,16,25,36]
    
    plt.vlines(x, 0, y, linestyle="dashed")
    plt.hlines(y, 0, x, linestyle="dashed")
    plt.scatter(x, y, zorder=2)
    
    plt.xlim(0,None)
    plt.ylim(0,None)
    plt.show()
    

提交回复
热议问题