Greg Gollaher
Greg Gollaher

Reputation: 11

Annotate each subplot from a list

I have a large dataframe that I need to make and annotate subplots from, see below. Everything seems ok, I still have some work to do on the x and y ticks but otherwise it seems pretty right, except, I can't get the annotation for each subplot to fall on the appropriate subplot. The annotations are all taken from the list 'df_cols' defined at the start. All of the annotation are plotted, but on top of one another in the lowest subplot, which is also the last subplot in the list.

df_cols = ['WC_015', 'WC_030', 'WC_046', 'WC_061', 'WC_076', 'WC_091', 'WC_107', 'WC_122', 'WC_137', 'WC_152', 'WC_168', 'WC_183', 'WC_213', 'WC_244', 'WC_274', 'WC_305', 'WC_366', 'WC_427', 'WC_488', 'WC_518']
fig, axs = plt.subplots(20, 1, figsize=(50,30))  #, sharex=True, sharey=True)

for k, col in enumerate(df_cols):
  df[col].plot(ax=axs[k])
  plt.annotate(df_cols[k], xy=(.01, .115), xycoords='axes fraction', fontsize=15, rotation=90, color='red')  #plt.annotate(df_cols[k], xy=(.01, .115), xycoords='axes fraction', fontsize=15, rotation=90, color='red')
fig.subplots_adjust(hspace=0)
plt.suptitle ('Volumetric Moisture Content', fontsize=40, y=.91)

plt.ylabel('Volumetric Moisture Content (m3/m3)', fontsize=30, y=10.0)
plt.xlabel('Date', fontsize=30)
plt.savefig('Moisture_Content_Plots.pdf', dpi=100, bbox_inches='tight')

Image of subplots

It seems like it should iterate through the labels with the loop that makes the subplots, but I can't get it there.

Upvotes: 0

Views: 35

Answers (1)

Greg Gollaher
Greg Gollaher

Reputation: 11

A big thanks to RuthC above, she had the answer. The following is what I used.

df_cols = ['WC_015', 'WC_030', 'WC_046', 'WC_061', 'WC_076', 'WC_091', 
'WC_107', 'WC_122', 'WC_137', 'WC_152', 'WC_168', 'WC_183', 'WC_213', 
'WC_244', 'WC_274', 'WC_305', 'WC_366', 'WC_427', 'WC_488', 'WC_518']
fig, axs = plt.subplots(20, 1, figsize=(50,30))  #, sharex=True, sharey=True)  20

for k, col in enumerate(df_cols):
  df[col].plot(ax=axs[k]) 
  axs[k].annotate(df_cols[k], xy=(.01, .115), xycoords='axes fraction', fontsize=15, rotation=90, color='red')
    
fig.subplots_adjust(hspace=0)
plt.suptitle ('Volumetric Moisture Content', fontsize=40, y=.91)

plt.ylabel('Volumetric Moisture Content (m3/m3)', fontsize=30, y=10.0)
plt.xlabel('Date', fontsize=30)
plt.savefig('Moisture_Content_Plots.pdf', dpi=100, bbox_inches='tight')

Plot with subplots annotated

Upvotes: 0

Related Questions