-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()
MSAxes
  • 15
  • 6

1 Answers1

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.

MaxPowers
  • 5,235
  • 2
  • 44
  • 69