【问题标题】:How to plot different dataframe in my code on each iteration?如何在每次迭代时在我的代码中绘制不同的数据框?
【发布时间】:2022-01-11 10:12:10
【问题描述】:

我希望我的图形采用特定布局,这是绘制该布局的代码

fig, axes = plt.subplots(figsize=(10, 10), ncols=2, nrows=4)
for i in range(4):
    for j in range(2):
        if i<j:
            axes[i, j].axis('off')
        else:
            print(i,j)
            axes[i, j].hist(df1.col1,bins=20,edgecolor='k')

但我想在每个子图(总共 7 个数据集)中绘制不同的数据集(df1.col1,df2.col1,...df7.col1)。

使用此代码我应该做哪些修改来绘制它??

【问题讨论】:

  • 不,这是为了方便子图布局

标签: python pandas matplotlib subplot


【解决方案1】:

您可能可以创建一个由这些列组成的数组,例如

data_frames = [df1.col1, df2.col1, df3.col1, df4.col1, df5.col1, df6.col1, df7.col1]

然后像这样索引到这个数组中:

axes[i, j].hist(dataframes[i*2+j], bins=20, edgecolor='k')

【讨论】:

    【解决方案2】:

    你可以试试这个列表,

    a=[df1,df2,df3,df4,df5,df6,df7]
    k=0
    fig, axes = plt.subplots(figsize=(10, 10), ncols=2, nrows=4)
    for i in range(4):
        for j in range(2):
            if i<j:
                axes[i, j].axis('off')
            else:
                print(i,j)
                # axes[i, j].hist(set_SS.depth,bins=20,edgecolor='k')
                
                axes[i, j].hist(a[k].col1,bins=20,edgecolor='k')
                k+=1
    

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2023-01-16
      相关资源
      最近更新 更多