首页 > 解决方案 > 绘制熊猫数据帧中的 Numpy 数组命令

问题描述

我正在使用 pandas 数据框和 numpy 打印子图。此代码每页打印 4 个图,然后将图保存为单独的 pdf。

我可以调用 numpy 数组以便以更有效的方式绘制 dataframe2 吗?也许使用 enumerate 命令?

我附上了我正在输出的图表的图片。

我的代码完成了工作,但是,我觉得总的来说效率很低。为了我自己的学习,我想简化代码。

Pandas_Outputs

no_of_graphs = 2*2

for a in range(2):

    start = 1  + counter * no_of_graphs 
    end = 4 + counter * no_of_graphs

    ax = df1[df1.columns[start:end]].plot(figsize=(12,15), grid = True, legend = True, subplots=True, layout = (2,2), sharex = False, color = 'blue')

    df2[df2.columns[1 + counter * no_of_graphs]].plot(ax=ax[0][0], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
    df2[df2.columns[2 + counter * no_of_graphs]].plot(ax=ax[0][1], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
    df2[df2.columns[3 + counter * no_of_graphs]].plot(ax=ax[1][0], grid = True, legend = True, linestyle = 'dashdot', color = 'red')
    df2[df2.columns[4 + counter * no_of_graphs]].plot(ax=ax[1][1], grid = True, legend = True, linestyle = 'dashdot', color = 'red')

    ax = np.squeeze(ax)
    fname = 'file' + str(a) + '.PDF'

    for y in range(0,2):
        for x in range(0,2):
            ax[x,y].set_xticks(df1.index[::60])
            ax[x,y].set_xticklabels(df1.Time[::60], rotation=270)
            plt.tight_layout()
            plt.gcf()
            plt.savefig(fname, dpi = 1000)

    counter +=1 
    print(counter)

标签: pythonpandasnumpydataframematplotlib

解决方案


我不确定我是否完全理解你的问题,但你肯定可以更有效地绘制你的第二个数据框。

axs = df1.plot(subplots=True, layout=(2,2), color = 'blue');
axs = df2.plot(subplots=True, layout=(2,2), ax=axs, linestyle = 'dashdot', color = 'red')

输出


推荐阅读