Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation does not appear in matplotlib plot

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?

like image 956
Michael Currie Avatar asked Jan 06 '14 18:01

Michael Currie


People also ask

How do I annotate in matplotlib?

Annotating with Arrow. The annotate() function in the pyplot module (or annotate method of the Axes class) is used to draw an arrow connecting two points on the plot. This annotates a point at xy in the given coordinate ( xycoords ) with the text at xytext given in textcoords .

How do I show all Xticks in matplotlib?

Use xticks() method to show all the X-coordinates in the plot. Use yticks() method to show all the Y-coordinates in the plot. To display the figure, use show() method.

Is PLT show () blocking?

show() and plt. draw() are unnecessary and / or blocking in one way or the other.


1 Answers

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.

like image 70
gg349 Avatar answered Sep 17 '22 13:09

gg349