Pyplot – format label in math mode of a variable

Question:

g = 0.0
for i in range(0,5,1):
    plt.plot(a,b,label='$K_{0:.2f}(x)$'.format(g))
    g += 0.31

I do not manage to get the whole number in subscript in math mode, it displays just the first in subscript.

How can this issue be solved?

Asked By: cerv21

||

Answers:

This should work fine:

plt.plot(a,b,label='$K_{%s}(x)$' % "{0:.2f}".format(g))
Answered By: Davide_sd