-1

I am making plots in a loop:

plotData.sort_values(by=['segment'])
for date in plotData.month_of_default.unique():
    plt.figure()
    temp =plotData[plotData.month_of_default==date][['New_Amount_2','ID','segment','total','payment','month']]
    denom = temp.drop_duplicates(subset=['ID']).groupby('segment')['total'].sum()

    test = temp.groupby(['segment','month']).New_Amount_2.sum().groupby(level=0).cumsum()/denom

    
plt.plot(test.unstack().T)

I've tried putting title='' in the plt.plot() brackets and in plt.figure(), also adding fig.subtitle('Title', fontsize=16) and neither worked - what's the right syntax to do this? Thanks! :)

BigBen
  • 46,229
  • 7
  • 24
  • 40
user13948
  • 443
  • 1
  • 6
  • 14
  • I think the function you want to use is `plt.title()`. You can add that anywhere below `plt.figure()`. – Jason Yu Feb 22 '23 at 16:59
  • Possible duplicate of [How to add a title to each subplot](https://stackoverflow.com/questions/25239933/how-to-add-a-title-to-each-subplot) – sbottingota Feb 22 '23 at 17:01
  • 1
    Does this answer your question? [How to add a title to each subplot](https://stackoverflow.com/questions/25239933/how-to-add-a-title-to-each-subplot) – Jody Klymak Feb 22 '23 at 17:09

1 Answers1

1

Adding a title to in matplotlib is done via the FigureBase object which would be returned by plt.figure() in your example. See these docs for more info: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/figure_title.html

In your example this would look like:

fig = plt.figure()
fig.suptitle("your plot title")

However if you're making multiple plots, using plt.subplots() to get access directly to the figure and axes objects would give you more direct control over their appearance. See: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html

jonchar
  • 6,183
  • 1
  • 14
  • 19