Plotting a line over several graphs

前端 未结 4 1460
孤城傲影
孤城傲影 2020-12-01 12:42

I don\'t know how this thing is called, or even how to describe it, so the title may be a little bit misleading.

The first attached graph was created with pyplot.

4条回答
  •  Happy的楠姐
    2020-12-01 12:56

    [Update 03/2013] In newer revisions of matplotlib, there's ConnectionPatch that greatly simplifies this task. It's particularly useful whenever there are more than two subplots that need to be covered.

    from matplotlib import pyplot as plt
    from matplotlib.patches import ConnectionPatch
    from numpy import arange, sin, cos
    
    xx = arange(100)
    cut = (xx > 0) & (xx % 17 == 0)
    y1 = sin(xx)
    y2 = (xx**2) % 2.0+cos(xx+0.5)
    
    fig = plt.figure()
    ax1 = fig.add_subplot(211)
    ax1.plot(xx, y1, c="blue")
    ax1.scatter(xx[cut], y1[cut], c="red")
    ax2 = fig.add_subplot(212)
    ax2.plot(xx, y2, c="green")
    ax2.scatter(xx[cut], y2[cut], c="red")
    
    for x in xx[cut]:
        con = ConnectionPatch(xyA=(x, -1.5), xyB=(x, 1.5),
            coordsA="data", coordsB="data", axesA=ax2, axesB=ax1,
            arrowstyle="-", linewidth=2, color="red")
        ax2.add_artist(con)
    
    plt.draw()
    fig.savefig('pic.png')
    

提交回复
热议问题