Skip to content

Failed to add annotate to figure #15745

Closed
@HookWang

Description

@HookWang

Bug report

Bug summary
I'm new to matplotlib
I want to add an annotate to figure through plt.gcf.texts.append(matplotlib.text.Annotation(...))
Maybe it is a wrong way.
It failed because when processing
Text.init(self, x, y, text, **kwargs) # text.py line 2168
self.arrow_patch is needed but not defined yet
self.arrow_patch will be defined at line 2184 or line 2187 in file text.py
so AttributeError: 'Annotation' object has no attribute 'arrow_patch'

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerPatch
import matplotlib.patches as mpatches
import matplotlib.text as mtext
import matplotlib.lines as mlines
import matplotlib.ticker as ticker
import matplotlib
import numpy as np


# class HandlerEllipse(HandlerPatch):
#     def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans):
#         center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
#         p = mpatches.Ellipse(xy=center, width=width + xdescent,
#                              height=height + ydescent)
#         self.update_prop(p, orig_handle, legend)
#         p.set_transform(trans)
#         return [p]


std_rect_prop = {'lw':1, 'facecolor':'none'}

ax = plt.subplot(111)
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)

plt.gcf().patches.append(mpatches.Circle((0, 0), 0.1,  transform=plt.gcf().transFigure, ec='C0', fc='C0', lw=1))
plt.gcf().texts.append(mtext.Text(x=0.1,y=0.03,text="plt.gcf().patches.append(mpatches.Circle((0, 0), 0.1,  transform=plt.gcf().transFigure, ec='C0', fc='C0', lw=1))\nplt.gcf().texts.append(mtext.Text(x=0.1,y=0.03,text='xx',transform=plt.gcf().transFigure, figure=plt.gcf(),bbox={'fc':'C0','alpha':0.5, 'pad':5})",
                                  transform=plt.gcf().transFigure,
                                  figure=plt.gcf(),
                                  bbox={'color':'C0','alpha':0.5, 'pad':5}))
# Failed Here
plt.gcf().texts.append(mtext.Annotation("123456",
            xy = (0,0),
            xytext = (0.5,0.5),
            arrowprops=dict(color='C2',shrink=0.05),
            #arrow_patch = mlines.Line2D,
            axes = ax,
            figure=plt.gcf(),
            bbox={'color':'C2', 'alpha': 0.5, 'pad': 5}))

ax.plot(0.1,0.1,'C1o',transform=ax.transAxes)
ax.annotate("ax.plot(0.1,0.1,'C1o',transform=ax.transAxes)",
            xy = ax.transData.inverted().transform(ax.transAxes.transform((0.1,0.1))),
            xytext = ax.transData.inverted().transform(ax.transAxes.transform((0.02,0.02))),
            arrowprops=dict(color='C1',shrink=0.05),
            bbox={'color':'C1', 'alpha': 0.5, 'pad': 5})

ax.plot(-0.5,-0.5,'C2o',label='234')
ax.annotate("ax.plot(0.1,0.1,'C2o',transform=ax.transAxes)",
            xy = (-0.5,-0.5),
            xytext = (-0.7,-0.7),
            arrowprops=dict(color='C2',shrink=0.05),
            bbox={'color':'C2', 'alpha': 0.5, 'pad': 5})

ax.add_patch(mpatches.Rectangle((0.1, 0.1), 0.8, 0.8,  transform=ax.transAxes, ec='C0', **std_rect_prop))
ax.annotate("ax.add_patch(mpatches.Rectangle((0.1, 0.1), 0.8, 0.8,  transform=ax.transAxes, ec='C0', **std_rect_prop))",
            xy = ax.transData.inverted().transform(ax.transAxes.transform((0.1,0.9))),
            xytext = ax.transData.inverted().transform(ax.transAxes.transform((0.2,0.8))),
            arrowprops=dict(ec='C0', fc='w', shrink=0.05),
            bbox={'color': 'C0', 'alpha': 0.5, 'pad': 5})

ax.add_patch(mpatches.Rectangle((-0.5, -0.5), 1, 1,  transform=ax.transData, ec='C1', **std_rect_prop))
ax.annotate("ax.add_patch(mpatches.Rectangle((-0.5, -0.5), 1, 1,  transform=ax.transData, ec='C1', **std_rect_prop))",
            xy = (-0.5, 0.5),
            xytext = (-0.4,0.4),
            arrowprops=dict(ec='C1', fc='w', shrink=0.05),
            bbox={'color': 'C1', 'alpha': 0.5, 'pad': 5})
# ax.add_patch(mpatches.Rectangle((0.1, 0.1), 0.8, 0.8,  transform=plt.gcf().transFigure, ec='C2', **std_rect_prop))
plt.gcf().patches.append(mpatches.Rectangle((0, 0), 1, 1,  transform=plt.gcf().transFigure, ec='C2', **std_rect_prop))
# ax.legend(loc='upper left', bbox_to_anchor=(1,1))
plt.show()

Actual outcome

Traceback (most recent call last):
  File "X:/toolKit/jupyter/tranform.py", line 40, in <module>
    bbox={'color':'C2', 'alpha': 0.5, 'pad': 5}))
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\deprecation.py", line 307, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\text.py", line 2168, in __init__
    Text.__init__(self, x, y, text, **kwargs)
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\text.py", line 168, in __init__
    self.update(kwargs)
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\text.py", line 177, in update
    super().update(kwargs)
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 974, in update
    ret = [_update_property(self, k, v) for k, v in props.items()]
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 974, in <listcomp>
    ret = [_update_property(self, k, v) for k, v in props.items()]
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 971, in _update_property
    return func(v)
  File "C:\Users\roger\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\text.py", line 2234, in set_figure
    if self.arrow_patch is not None:
AttributeError: 'Annotation' object has no attribute 'arrow_patch'

Expected outcome

Matplotlib version

  • Operating system: win7 64
  • Matplotlib version: 3.1.1
  • Matplotlib backend (print(matplotlib.get_backend())):TkAgg
  • Python version: 3.6.4
  • Jupyter version (if applicable):
  • Other libraries:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions