【问题标题】:Can I change the arrowprops (properties) of an annotation in an animation?我可以更改动画中注释的箭头属性(属性)吗?
【发布时间】:2017-11-23 02:42:18
【问题描述】:

我一直在寻找一种方法来更改使用动画 (matplotlib) 中注释的 arrowprops 参数创建的箭头的属性。

箭头是这样创建的:

axes.annotate('', xy=(-1, -1), xytext=(-1, -1), xycoords='data', textcoords='data',
                  arrowprops=dict(arrowstyle='<-', color='blue', linewidth=2))

我希望能够在动画期间更改箭头的颜色。

【问题讨论】:

    标签: python python-2.7 animation matplotlib


    【解决方案1】:

    注解的箭头存储在arrow_patch属性中。你可以通过set_color设置它的颜色。

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    fig, ax = plt.subplots()
    ax.axis([-2,2,-2,2])
    
    arrowprops=dict(arrowstyle='<-', color='blue', linewidth=10, mutation_scale=150)
    an = ax.annotate('Blah', xy=(1, 1), xytext=(-1.5, -1.5), xycoords='data', 
                     textcoords='data', arrowprops=arrowprops)
    
    colors=["crimson", "limegreen", "gold", "indigo"]
    def update(i):
        c = colors[i%len(colors)]
        an.arrow_patch.set_color(c)
    
    ani = animation.FuncAnimation(fig, update, 10, interval=1000, repeat=True)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2014-10-17
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      相关资源
      最近更新 更多