Adding line to scatter plot using python's matplotlib

前端 未结 2 1266
旧时难觅i
旧时难觅i 2021-02-07 12:03

I am using python\'s matplotlib and want to create a matplotlib.scatter() with additional line. The line should proceed from the lower left corner to the upper righ

2条回答
  •  隐瞒了意图╮
    2021-02-07 12:33

    This draws a diagonal line which is independent of the scatter plot data and which stays rooted to the axes even if you resize the window:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.lines as mlines
    import matplotlib.transforms as mtransforms
    
    x, y = np.random.random((2, 100))*2
    fig, ax = plt.subplots()
    ax.scatter(x, y, c='black')
    line = mlines.Line2D([0, 1], [0, 1], color='red')
    transform = ax.transAxes
    line.set_transform(transform)
    ax.add_line(line)
    plt.show()
    

提交回复
热议问题