Search code examples
pythonmatplotlibplottextsymbols

Python pyplot text with square root


I'm trying to annotate my pyplot with the following: enter image description here

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

Solution

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