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?
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 .
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.
show() and plt. draw() are unnecessary and / or blocking in one way or the other.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With