0

How can I add titles to each subplot here?

 fig, axs = plt.subplots(ncols=4, nrows=2, figsize=(16,8))
 for city, nrow in zip(['City1', 'City1'], [0, 1]):
    df = data[(data.city==city)&(data.sdate.dt.year==2021)]
    for col, ncol in zip(['parameterX', 'parameterY', 'parameterZ', 'parameterH'], [0,1, 2,3]):
    axs[nrow, ncol].hist(df[col], bins=50)

enter image description here

Michael S.
  • 3,050
  • 4
  • 19
  • 34
Ann
  • 73
  • 1
  • 6

2 Answers2

2

just do at the end

fig, axs = plt.subplots(ncols=4, nrows=2, figsize=(16,8))
 for city, nrow in zip(['City1', 'City1'], [0, 1]):
    df = data[(data.city==city)&(data.sdate.dt.year==2021)]
    for col, ncol in zip(['parameterX', 'parameterY', 'parameterZ', 'parameterH'], [0,1, 2,3]):
        axs[nrow, ncol].hist(df[col], bins=50)
        axs[nrow, ncol].set_title('my_subplot_title') 
Lucas M. Uriarte
  • 2,403
  • 5
  • 19
1

Customize the title with column, row name and add it as below.

fig, axs = plt.subplots(ncols=4, nrows=2, figsize=(16,8))
for city, nrow in zip(['City1', 'City1'], [0, 1]):
    df = data[(data.city==city)&(data.sdate.dt.year==2021)]
    for col, ncol in zip(['parameterX', 'parameterY', 'parameterZ', 'parameterH'], [0,1, 2,3]):
        axs[nrow, ncol].hist(df[col], bins=50)
        title=city+'_'+col
        axs[nrow, ncol].set_title(title)
        
band
  • 129
  • 7