4

I want to insert text inside a latex equation which is actually a title for the plot. In other words, the following is the python code for the title I am using:

import matplotlib.pyplot as plt

x=8
plt.title(r"Actual time-series is $Y_t=\alpha^\top X_t + \beta^\top X_{t-{0}}$".format(x))

plt.show()

The following is the error I received when I implemented the above code:

 Traceback (most recent call last):
  File "switch_systems.py", line 419, in <module>
     plt.title(r"Actual time-series is $Y_t=\alpha^\top X_t + \beta^\top  X_{t-{0}}$".format(x)
   ValueError: unexpected '{' in field name

I want my title to look like Actual time-seris is $Y_t=\alpha^\top X_t + \beta^\top X_{t-8}$, as in the text inside latex to be exactly resemble what it would have looked like had it been written in latex. Is there a trick to do this?

1

1 Answer 1

6

You need to escape the literal { that you want to keep in the text by doing {{ and }}

r"Actual time-series is $Y_t=\alpha^\top X_t + \beta^\top X_{{t-{0}}}$".format(8)

results in

Actual time-series is $Y_t=\alpha^\top X_t + \beta^\top X_{t-8}$
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.