david
david

Reputation: 201

Matplotlib plot does not show legend

I want to quickly plot a function varying one of its parameters and with a legend showing the value of this parameter:

def supergaussian1d(x, x0, Imax, FWHM, n):
    return Imax * np.exp(-4 * np.log(2) * (np.sqrt((x + x0)**2) / FWHM) ** (2*n))

x = np.linspace(0, 15, num=100)
for m in range(2, 9):
    plt.plot(x, supergaussian1d(x, x0=-7, Imax=10, FWHM=5, n=m), label='{}'.format(m))
    plt.ylim((0, 12))
plt.show()

This is the output:

enter image description here

Why is the legend not displayed?

Also, does it make a difference whether or not plt.show() is indented?

Upvotes: 0

Views: 241

Answers (1)

Dmitry Petrov
Dmitry Petrov

Reputation: 120

Have you tried adding plt.legend() before plt.show()?

Upvotes: 2

Related Questions