简体   繁体   中英

Subplot charts plotted within a loop very squashed

I have a dataframe with ~120 features that I would like to examine by year. I am plotting each feature, x = year, y = feature value within a loop. Whilst these plot successfully, the charts are illegible as they are totally squashed.

I have tried using plt.tight_layout() and adjusting the figure size using plt.rcParams['figure.figsize'] but sadly to no avail

for i in range(len(roll_df.columns)):
  plt.subplot(len(roll_df.columns), 1, i+1)
  name = roll_df.columns[i]
  plt.plot(roll_df[name])
  plt.title(name, y=0)
  plt.yticks([])
  plt.xticks([])
  plt.tight_layout()
  plt.show()

The loop runs but all plots are so squashed on the y-axis as to become illegible:

情节

Matplotlib will not automatically adjust the size of your figure. So if you add more subplots below each other, it will split the available space instead of extending the figure. That's why your y axes are so narrow.

You could try to define the figure size beforehand, or determine the figure size based on how many subplots you have:

n_plots = roll_df.shape[1]
fig, axes = plt.subplots(n_plots, 1, figsize=(8, 4 * n_plots), tight_layout=True)

# Then your usual part, but plot on the created axes
for i in range(n_plots):
  name = roll_df.columns[i]
  axes[i].plot(roll_df[name])
  axes[i].title(name, y=0)
  axes[i].yticks([])
  axes[i].xticks([])

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM