Pyplot: vertical gradient fill under curve?

前端 未结 2 1095
礼貌的吻别
礼貌的吻别 2020-11-28 15:40

I\'m wondering if there\'s a way to fill under a pyplot curve with a vertical gradient, like in this quick mockup:

\

2条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 16:14

    There may be a better way, but here goes:

    from matplotlib import pyplot as plt
    
    x = range(10)
    y = range(10)
    z = [[z] * 10 for z in range(10)]
    num_bars = 100  # more bars = smoother gradient
    
    plt.contourf(x, y, z, num_bars)
    
    background_color = 'w'
    plt.fill_between(x, y, y2=max(y), color=background_color)
    
    plt.show()
    

    Shows:

    enter image description here

提交回复
热议问题