Home > Software engineering >  Addition title to plot
Addition title to plot

Time:09-09

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

CodePudding user response:

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') 

CodePudding user response:

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)
        
  • Related