多个数据帧的子图

问题描述 投票:0回答:1

我在一个文件夹中有多个数据帧,因此编写了一个迭代 for 循环,用于从文件夹导入和读取每个数据集,但是使用子图绘制每个数据集,以便将它们绘制为 2 行、3 列子图、一个列表创建了数据帧,但是当调用函数对数据帧进行子图绘制时,会发生此错误“AttributeError:'numpy.ndarray'对象没有属性'plot'”,有人知道如何使该图工作吗?

` numRows =2
    numCols =3
    print(type(numRows), type(numCols))
    figure, ax = plt.subplots(numRows, numCols)
    i =0
    print(type(i))

    for data in dataFrameList:
        dataFrameList=pd.DataFrame(data)
        #print(type(dataFrameList))
        row = i // numCols
        col = i % numCols

        ax = ax.plot(row,col)
        ax.plot(dataFrameList[i]['a'], dataFrameList[i]['b'])
        ax.set_title("DataFrame Name = " + str(i))

        # Adjust the layout and spacing
        plt.tight_layout()

        data += 1
        # Combine all the operations and display
        plt.show()`

我期待保存在 dataFrameList 中的数据帧的子图


` numRows =2
    numCols =3
    print(type(numRows), type(numCols))
    figure, ax = plt.subplots(numRows, numCols)
    i =0
    print(type(i))

    for data in dataFrameList:
        dataFrameList=pd.DataFrame(data)
        #print(type(dataFrameList))
        row = i // numCols
        col = i % numCols

        ax = ax.plot(row,col)
        ax.plot(dataFrameList[i]['a'], dataFrameList[i]['b'])
        ax.set_title("DataFrame Name = " + str(i))

        # Adjust the layout and spacing
        plt.tight_layout()

        data += 1
        # Combine all the operations and display
        plt.show()`
python dataframe plot attributeerror subplot
1个回答
0
投票

您的代码中有相当多的错误,您应该提供完整的代码,因为我们不知道

dataFrameList
中的内容以及哪些行引发错误。

无论如何,我看到的主要问题是您从

data
获得
dataFrameList
但随后您使用该变量来存储数组。 此外,当你想调用子图中的位置时,你应该使用
ax[i, j].plot(whatever you want to plot)
。您也不会更新绘图的索引,并且不需要
data += 1
,因为如果您不更改
dataFrameList
,则会通过 for 循环进行更新。最后,图中对数据框的调用不正确,并且
show()
tight_layout()
应仅在 for 循环末尾应用。

这里有一些固定的东西,但由于我们看不到整个代码,因此不一定能工作。我更新绘图索引的方式不是最好的方式,但由于我们不知道

dataFrameList
的结构,所以很难正确地做到这一点。

numRows = 2
numCols = 3
figure, ax = plt.subplots(numRows, numCols)
i = 0
j = 0
flag = True

for data in dataFrameList:
    df=pd.DataFrame(data)

    ax[i, j].plot(df['a'], df['b'])
    ax[i, j].set_title("DataFrame Name = " + str(data))
    
    if flag:
       i += 1
       flag = False
    else:
       j += 1
       i = 0
       flag = True

# Adjust the layout and spacing
plt.tight_layout()

# Combine all the operations and display
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.