0

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.

3
  • 2
    pyplot functions work on the current axes. If you do not explicitly set the current axes with plt.sca then it will be the most recently created one. You could set the current axes in your loop or, more simply, use axs[k].annotate. pyplot functions are mostly wrappers of figure and axes methods, see matplotlib.org/stable/users/explain/figure/api_interfaces.html Commented Jan 4 at 9:41
  • You used sharey=True, but the limits of the 20 y axes are different in the figure you've posted. Commented Jan 5 at 10:27
  • Thanks RuthC, that was it. Please see the answer below. Commented Jan 6 at 4:15

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.