Home > Net >  Matplotlib Annotations disappear during animation when updating xlim
Matplotlib Annotations disappear during animation when updating xlim

Time:10-23

I am having an issue with FuncAnimation where my annotations are removed after updating xlim. Here is the code with a preview underneath

You can try the code in a google colab here FuncAnimation-annotated-static-axes.gif

  1. FuncAnimation-annotated-static-x.gif
    • xlim is fixed
    • ylim is dynamic

FuncAnimation-annotated-static-x.gif

  1. FuncAnimation-annotated-static-y.gif
    • xlim is dynamic
    • ylim is fixed

FuncAnimation-annotated-static-y.gif

  1. FuncAnimation-annotated-dynamic-axes.gif
    • xlim and ylim are dynamic

FuncAnimation-annotated-dynamic-axes.gif

My annotations disappear in the two cases where the xlim is updated:

  1. FuncAnimation-annotated-static-y.gif
  2. FuncAnimation-annotated-dynamic-axes.gif

Note that when xlim is static this doesn't happen:

  1. FuncAnimation-annotated-static-axes.gif
  2. FuncAnimation-annotated-static-x.gif

Does anyone know why this happens or how to update the xlim without removing annotations?

Please let me know if something is unclear / worded poorly as I really need to solve this.

CodePudding user response:

So the issue is with how I was moving my annotation. This is the fix:

    # Don't do this - updating xlim will causing the annotation do disappear 
    # first_image_annotation_xybox = (x1_and_x2, y1)
    # first_image_annotation.xybox = first_image_annotation_xybox
    #
    # second_image_annotation_xybox = (x1_and_x2, y2)
    # second_image_annotation.xybox = second_image_annotation_xybox

    for lnum, line in enumerate(lines):
        # set data for each line separately.
        line.set_data(xlist[lnum], ylist[lnum])


    # Do this - Update our annotations
    for ann in ann_list:
        ann.remove()
    ann_list = []
    first_image_annotation = AnnotationBbox(
        first_offset_image, (x1_and_x2, y1), xycoords='data', frameon=False)
    ann_list.append(ax1.add_artist(first_image_annotation))

    second_image_annotation = AnnotationBbox(
        second_offset_image, (x1_and_x2, y2), xycoords='data', frameon=False)
    ann_list.append(ax1.add_artist(second_image_annotation))

    return lines, ann_list

The rest of the code is the same. Wonder why this happens on updating xlim and not on updating ylim ¯\(ツ)

  • Related