Connect points with horizontal lines

前端 未结 2 583
星月不相逢
星月不相逢 2021-01-16 08:13

The goal is to fill the space between two arrays y1 and y2, similar to matplotlib\'s fill_between. But I don\'t want to fill the space with a polyg

2条回答
  •  Happy的楠姐
    2021-01-16 08:59

    Using a LineCollection could be handy if there are lots of lines in the game. Similar to the other answer, but less expensive:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.collections import LineCollection
    
    def draw_lines_between(*, x1=None, x2=None, y1, y2, ax=None, **kwargs):
        ax = ax or plt.gca()
        x1 = x1 if x1 is not None else np.arange(len(y1))
        x2 = x2 if x2 is not None else x1
    
        cl = LineCollection(np.stack((np.c_[x1, x2], np.c_[y1, y2]), axis=2), **kwargs)
        ax.add_collection(cl)
        return cl
    
    n = 10
    y1 = np.random.random(n)
    y2 = np.random.random(n) + 1
    x = np.arange(n)
    color_list = [str(x) for x in np.round(np.linspace(0., 0.8, n), 2)]
    
    fig, ax = plt.subplots()
    ax.plot(x, y1, 'r')
    ax.plot(x, y2, 'b')
    draw_lines_between(ax=ax, x1=x, y1=y1, y2=y2, colors=color_list)
    
    plt.show()
    

提交回复
热议问题