WebOrCode
WebOrCode

Reputation: 7312

Simple matplotlib Annotating example not working in Python 2.7

Code

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = ax.plot(t, s, lw=2)

ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
            arrowprops=dict(facecolor='black', shrink=0.05),
            )

ax.set_ylim(-2,2)
plt.show()

from http://matplotlib.org/1.2.0/users/annotations_intro.html
return

TypeError: 'dict' object is not callable

I manged to fixed it with

xxx={'facecolor':'black', 'shrink':0.05}
ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
            arrowprops=xxx,
            )

Is this the best way ?
Also what caused this problem ? ( I know that this started with Python 2.7)

So if somebody know more, please share.

Upvotes: 1

Views: 1205

Answers (1)

Bonlenfum
Bonlenfum

Reputation: 20205

Since the code looks fine and runs ok on my machine, it seems that you may have a variable named "dict" (see this answer for reference). A couple of ideas on how to check:

  • use Pylint.
  • if you suspect one specific builtin, try checking it's type (type(dict)), or look at the properties/functions it has (dir(dict)).
  • open a fresh notebook and try again, if you only observe the problem in interactive session.
  • try alternate syntax to initialise the dictionary

    ax.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
        arrowprops={'facecolor':'black', 'shrink':0.05})
    
  • try explicitly instancing a variable of this type, using the alternate syntax (as you did already).

Upvotes: 1

Related Questions