Search code examples
pythonmatplotlibstring-formatting

Pyplot - format label in math mode of a variable


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?


Solution

  • This should work fine:

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