Skip to content

set_position on Annotation is not working #2329

Closed

Description

@alefnula

annotation.set_position(...) is not working in versions 1.2.1, 1.3.0 nor on the master.

Here is an example that demonstrates this.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# Create initial data
data = np.array([[1,2,3,4,5], [7,4,9,2,3]])

# Create figure and axes
fig = plt.figure()
ax = plt.axes(xlim=(0, 20), ylim=(0, 20))

# Create initial objects
line, = ax.plot([], [], 'r-')
annotation = ax.annotate('A0', xy=(data[0][0], data[1][0]))
annotation.set_animated(True)

def init():
    return line, annotation

def update(num):
    newData = np.array([[1 + num, 2 + num / 2, 3, 4 - num / 4, 5 + num],
                        [7, 4, 9 + num / 3, 2, 3]])
    line.set_data(newData)
    # This is not working but in docs it is described as a setter
    # annotation.set_position((newData[0][0], newData[1][0]))
    # This works but this is described in docs as getter
    annotation.xytext = (newData[0][0], newData[1][0])
    return line, annotation

anim = animation.FuncAnimation(fig, update, frames=25, init_func=init,
                               interval=200, blit=True)
plt.show()

SO Question where the bug was discovered: Animate points with labels with mathplotlib

Activity

  1. mdboom commented on Aug 26, 2013

    @mdboom
    Member

    I think the issue here is that set_position works only on raw text, not on an annotation. I think it's a matter of making the api more consistent between the two.

  2. tacaswell commented on Aug 27, 2013

    @tacaswell
    Member

    It looks like Annotation inherits from both Text and _AnnotationBase both of which maintain location information of the text. The set_position function updates the data structures used by the Text object. They look like they work, but that data is never used in Annotation.draw so it has no effect.

    _AnnotationBase is also used as a baseclass for AnnotationBbox.

    I would propose pulling xytext and xytextcoords out of _AnnotationBase and making keeping track of where the other end of the annotation is the job of sub-classes.

  3. tacaswell commented on Nov 27, 2013

    @tacaswell
    Member

    @alefnula I think this is fixed on master. Please check and if it is not we will reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions