7

The annotation "test" displays if I run the following code:

import matplotlib.pyplot as plt
plt.figure()
ax = plt.gca()
ax.annotate("Test", xy=(0.2, 0.2))

However, the exact same code will not display the annotation if I call plt.plot() instead of plt.figure():

import matplotlib.pyplot as plt
plt.plot()
ax = plt.gca()
ax.annotate("Test", xy=(0.2, 0.2))

Why does the second code block not show the annotation?

8
  • I see the annotation with both code blocks when calling plt.show() at the end. Are you using an interactive environment? Commented Jan 6, 2014 at 18:26
  • matplotlib.backends.backend returns 'Qt4Agg'. matplotlib.is_interactive() returns True. Adding plt.show() at the end still leaves the annotation as not displayed for me. Commented Jan 6, 2014 at 18:31
  • I still can't reproduce the problem with either TkAgg or Qt4Agg, version 1.2.1 on Linux but I get matplotlib.is_interactive()==False with the default setup. Perhaps it is something to do with interactive mode? Commented Jan 6, 2014 at 18:39
  • 1
    On my machine, simply calling figure() sets xlim and ylim to [0,1] in the first example with the text within the domain, while in the second example the annotated test is outside the xlim and ylim, that are set to [-.06,.06]. Is this your problem? Commented Jan 6, 2014 at 18:55
  • 1
    I'll post it as the answer then Commented Jan 6, 2014 at 19:01

1 Answer 1

6

In the first example, calling figure() sets xlim and ylim to [0,1] with the text within the domain, at [.2,.2].

In the second example the annotated test is outside the xlim and ylim. They are set automatically to [-.06,.06] (at least on my machine).

In the second example, simply invoke

ax.set_xlim(-.4,.4)
ax.set_xlim(-.4,.4)

and the annotation will appear in the figure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.