Python Pyplot Text with Square Root

I'm trying to annotate my pyplot with the following:

I am able to get the square root of a number, and I am able to get the h-squared, but when I put them together I get a

ValueError: cannot switch from automatic field numbering to manual field specification

Any idea what I'm doing wrong?

import matplotlib.pyplot as plt
x = [1, 2, 3]
y = np.array([[1, 2], [3, 4], [5, 6]])
plt.plot(x, y)
t=97.35713828629935
plt.text(1,5,r'{:3.1f}% greater than $$\sqrt{0.5*$h^2$}$$'.format(t))

1 Answer

The problem is the second pair of {} confuses .format. Use double {{}} to escape them.

plt.text(1,5,r'{:3.1f}% greater than $\sqrt{{0.5 * h^2}}$'.format(t))

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest