简体   繁体   English

循环内绘制的子图图表非常紧凑

[英]Subplot charts plotted within a loop very squashed

I have a dataframe with ~120 features that I would like to examine by year. 我有一个具有约120个功能的数据框,我想按年份进行检查。 I am plotting each feature, x = year, y = feature value within a loop. 我正在绘制每个特征,x =年,y =特征值。 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 我试过使用plt.tight_layout()并使用plt.rcParams ['figure.figsize']调整图形大小,但可惜无济于事

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: 循环运行,但所有图都在y轴上被压缩,以致变得难以辨认:

情节

Matplotlib will not automatically adjust the size of your figure. Matplotlib不会自动调整图形的大小。 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. 这就是为什么您的y轴是如此之窄。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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