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.
解决方案
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()