I have a very basic question : how to do a line break with matplotlib in python with an "annotate" command. I tried "\" and "\n" but it does not work. And how to do this for a "Latex" annotation and for a normal text annotation ?
Thank you very much.
Matplotlib can use LaTeX to render text. This is activated by setting text. usetex : True in your rcParams, or by setting the usetex property to True on individual Text objects.
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 .
What exactly did you try?
Were you, by chance, using a raw string (e.g. r"whatever"
)?
'\n'
works perfectly, but if you're using a raw string to avoid latex sequences being interpreted as an escape, it will be interpreted by python as '\'
and 'n'
instead of a newline.
As an example:
import matplotlib.pyplot as plt plt.annotate('Testing\nThis\nOut', xy=(0.5, 0.5)) plt.show()
On the other hand, if we use a raw string:
import matplotlib.pyplot as plt plt.annotate(r'Testing\nThis\nOut', xy=(0.5, 0.5)) plt.show()
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