如何使用 for 循环 "fill in" 一个一个地绘制子图?
How to "fill in" subplots one by one using a for loop?
我目前正在尝试制作子图,然后使用循环填充它们。更具体地说,我有大约 50 个 CSV 文件,每个文件都有两列。我想为每个文件使用两列创建一个散点图,从而产生 50 个图。我的代码是:
files = os.listdir() # List to contain the 50 file names.
fig, axes = plt.subplots(nrows=10, ncols=5, figsize=(45, 30))
axes = axes.ravel()
for idx, filename in enumerate(range(1, len(files) + 1)):
df = pd.read_csv(files[idx - 1]) # Read in the appropriate file.
axes[idx] = sns.scatterplot(x='pred', y='obs', data=df, alpha=0.3)
axes[idx].set_title('Plot {}'.format(idx), fontsize='x-large')
如果我 运行 这段代码,我得到 49 个空子图,所有数据都重叠到最后一个子图中。我将如何实现我想要的?提前致谢。
您需要将轴作为参数传递给 scatterplot
sns.scatterplot(x='pred', y='obs', data=df, alpha=0.3, ax=axes[idx])
我目前正在尝试制作子图,然后使用循环填充它们。更具体地说,我有大约 50 个 CSV 文件,每个文件都有两列。我想为每个文件使用两列创建一个散点图,从而产生 50 个图。我的代码是:
files = os.listdir() # List to contain the 50 file names.
fig, axes = plt.subplots(nrows=10, ncols=5, figsize=(45, 30))
axes = axes.ravel()
for idx, filename in enumerate(range(1, len(files) + 1)):
df = pd.read_csv(files[idx - 1]) # Read in the appropriate file.
axes[idx] = sns.scatterplot(x='pred', y='obs', data=df, alpha=0.3)
axes[idx].set_title('Plot {}'.format(idx), fontsize='x-large')
如果我 运行 这段代码,我得到 49 个空子图,所有数据都重叠到最后一个子图中。我将如何实现我想要的?提前致谢。
您需要将轴作为参数传递给 scatterplot
sns.scatterplot(x='pred', y='obs', data=df, alpha=0.3, ax=axes[idx])