2

I want to write some text on a matplotlib axis. This text contains a linebreak and should be accessible to LaTex text-rendering.

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as mpl

mpl.rc('font', family='sans-serif')
mpl.rc('text', usetex=True)

fig = mpl.figure()
ax = fig.add_subplot(1,1,1)

text = (r"This is the first line"
         "\n"
         r"And here comes the second one")

ax.text(0.2,0.5,text)

produces the following output:

enter image description here

Now I want to plug the two sentences together such that I am flexible regarding the different inputs of the complete text. I was thinking about somethin like

text_1 = "This is the first line"
text_2 = "And here comes the second one"

completeText = [text_1, text_2]

textIn = '⧹n'.join(map(str, completeText))

ax.text(0.2,0.5,textIn)

However, this produces the error

UnicodeEncodeError: 'ascii' codec can't encode character '\u29f9' in position 300: ordinal not in range(128)

Additionally, is it possible to put a bullet point like in LaTex itemize in front of every new line?

0

1 Answer 1

2

Check your '\n',make sure it's not '⧹n'.

I used '\n' to replace '⧹n' in your code.And the code was running successfully.

The difference is that \

About the bullet,try this,it work on my ipython notebook.

%matplotlib inline    # this line is for my ipython notebook,you don't need it

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as mpl

mpl.rc('font', family='sans-serif')
mpl.rc('text', usetex=True)

fig = mpl.figure()
ax = fig.add_subplot(1,1,1)

text_1 = r"{\bullet}This is the first line"
text_2 = r"{\bullet}And here comes the second one"

completeText = [text_1, text_2]

textIn = '\n'.join(map(str, completeText))

ax.text(0.2,0.5,textIn)


fig.show()
Sign up to request clarification or add additional context in comments.

2 Comments

You now, there are days when I just need to put on my glasses before starting to work... Thanks though.
@Pat Add a {\bullet} in front of the line may be helpful.And don't forget the r before the whole line.I have improved my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.