代码之家  ›  专栏  ›  技术社区  ›  Erica Belcher

如何将线条批注添加到具有多条线条的动画线条图中?

  •  0
  • Erica Belcher  · 技术社区  · 3 年前

    我想为动画matplotlib线条图上的每一行添加注释。我想展示鸟类种类(如湿地、海鸟等)。有人能帮忙吗?我尝试过使用现有的解决方案,但不知道如何将它们整合到我的代码中。 非常感谢。

    from matplotlib import animation
    from matplotlib.animation import FuncAnimation
    from itertools import count
    %matplotlib qt
    
    l1 = df["all_species"]
    l2 = df["farmland"]
    l3 = df["woodland"]
    l4 = df["wetland"]
    l5 = df["seabirds"]
    l6 = df["winteringwater"]
    l7 = df["wildfowl"]
    l8 = df["wader"]
    xval = df["Year"]
    x1,y1,y2,y3,y4,y5,y6,y7,y8 = [], [], [], [], [], [], [], [], []
    
    fig, axes = plt.subplots(nrows = 1, ncols = 1, figsize = (15,7.5))
    axes.set_ylim(0, 250)
    axes.set_xlim(1970, 2020)
    plt.style.use("ggplot")
    plt.title('The index chart visualises the change in the UK wild bird population based on 1970 
    as an index year. The index is published by the Birtish Trust for' 
          "\n" 'Ornithology (BTO) and the Royal Society for the Protection of Birds (RSPB).', 
    fontsize=10, loc="left")
    plt.suptitle('Population of wild birds: 1970 to 2019',fontsize=16, y=0.99, fontweight="bold", 
    horizontalalignment='right')
    axes.set_xlabel('Year', fontsize=10)
    axes.set_ylabel('Index value 1970=100', fontsize=10)
    plt.axhline(100, linestyle='--', linewidth=1, alpha=0.7, color='#679186')
    
    def animate(i):
    
     x1.append(1970+i)
     y1.append((l1[i]))
     y2.append((l2[i]))
     y3.append((l3[i]))
     y4.append((l4[i]))
     y5.append((l5[i]))
     y6.append((l6[i]))
     y7.append((l7[i]))
     y8.append((l8[i]))
    
     axes.plot(x1,y1, color="#454d66", linewidth=2, label='All species')    
     axes.plot(x1,y2, color="#309975", linewidth=2, label='Farmland')
     axes.plot(x1,y3, color="#58b368", linewidth=2, label='Woodland')
     axes.plot(x1,y4, color="#dad873", linewidth=2, label='Wetland')
     axes.plot(x1,y5, color="#ffca7a", linewidth=2, label='Seabirds')
     axes.plot(x1,y6, color="#2c698d", linewidth=2, label='Wintering Water')
     axes.plot(x1,y7, color="#e3f6f5", linewidth=2, label='Wildfowl')    
     axes.plot(x1,y8, color="#bae8e8", linewidth=2, label='Wader')
     ax.legend()
    
    
    anim = FuncAnimation(fig, animate, interval=1)
    
    0 回复  |  直到 3 年前