Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Matplotlib annotate line break (with and without latex)

Tags:

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.

like image 921
Vincent Avatar asked Apr 23 '12 16:04

Vincent


People also ask

Does Matplotlib use LaTeX?

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.

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 .


1 Answers

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() 

enter image description here

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() 

enter image description here

like image 108
Joe Kington Avatar answered Oct 10 '22 19:10

Joe Kington