-2

I am testing a graph and everything except the legend works. I've run the code below without the label found in the plt.plot but I still don't get a legend returned. Can anyone see why?

plt.figure(figsize=(8,5))

 
plt.title('Test Graph', fontdict={'fontweight':'bold', 'fontsize': 24})


plt.plot(excl.Date, excl.Pop2020, 'g.-', label = 'test')

plt.xticks(excl.Date[::4])

plt.xlabel('Date')
plt.ylabel('Pop')

plt.legend

plt.savefig('testpic.png', dpi = 300)

plt.show()
1
  • 2
    plt.legend() maybe? Commented Dec 28, 2020 at 19:12

1 Answer 1

0

In you example, you forgot the () at the end end of the function call to legend. Additionally, I would suggest to use the matplotlib's object-oriented API:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)

fig, ax = plt.subplots(1)
ax.plot(x, 'g.-', label='test')
ax.legend()

For further information on how legend works, visit the documentation.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.