1

I'm trying to save a matplotlib time series plot as png. When I generate the graph I produce also the legend, but I cannot save it in in the .png figure. I cannot save the legend.

data_df['point'].plot(linestyle='dashed', label='points')
plt.title('trial')
plt.ylabel('N. of points')
plt.xlabel('Time (days)')
plt.legend(loc='best', frameon=True)
plt.show(block = True)
plt.savefig('figures\\'+'case_1'+'.png', format ='png')

The data looks like:

Date
2010-01-06    7.54
2010-02-06    9.09
2010-03-06    7.91
2010-04-06    13.27
2010-05-06    7.86
               ...  
2020-03-19    11.95
2020-03-20    8.17
2020-03-21    10.29
2020-03-22    15.86
2020-03-23    7.36
Name: point, Length: 3324, dtype: float64

Does anyone know how to keep the legend in my .png figure?

srgam
  • 366
  • 1
  • 13

1 Answers1

0

If you pass bbox_inches='tight' to .savefig(), it should keep the legend close enough not to get cut off.

Peritract
  • 761
  • 5
  • 13