如何删除空白地块?

时间:2018-08-14 20:22:29

标签: python loops dataframe matplotlib

我正在尝试每次迭代制作一个新图形。但是我确实在每个循环中都有一个空白图。 我该如何解决? 这是我的代码。

import datetime
def make_plot(item, ap2id):
    ref_data1_pred = res[(res['ITEM'] == item) & (res['AP2ID'] == ap2id) & (res['WEEK'] >= '201701')]['QTY_PRED']
    ref_data1_real = res[(res['ITEM'] == item) & (res['AP2ID'] == ap2id) & (res['WEEK'] >= '201701')]['QTY_AP2']
    ref_data1_x = res[(res['ITEM'] == item) & (res['AP2ID'] == ap2id) & (res['WEEK'] >= '201701')]['WEEK']
    plt.figure()
    plt.plot(ref_data1_x, ref_data1_pred, 'r')
    plt.plot(ref_data1_x, ref_data1_real, 'b')
    plt.xticks(rotation=70)
    plt.xlabel("week")
    plt.ylabel("Qty")
    plt.title(item + " - " + ap2id)
#     plt.savefig("../image/qty/Graph_" + str(datetime.datetime.now()) +"_" + ap2id +".png", format="PNG")
    plt.show()

for item in res['ITEM'].drop_duplicates():
    for ap2id in res['AP2ID'].drop_duplicates():
        make_plot(item, ap2id)

我正在制作销售预测模型,并尝试比较预测数量和实际数量。数据如下。 x轴为周,y值为“ qty_ap2”和“ qty_pred”

df = pd.DataFrame(columns=['WEEK', 'ITEM', 'AP2ID', 'QTY_AP2', 'QTY_PRED'])
df.loc[1]=['201801', 'a', '1', 5, 2]
df.loc[2]=['201802', 'a', '1', 8, 5]
df.loc[3]=['201803', 'a', '1', 1, 6]
df.loc[4]=['201804', 'a', '1', 0, 2]
df.loc[5]=['201805', 'a', '1', 4, 3]

谢谢。

Plot example

1 个答案:

答案 0 :(得分:0)

以下代码对我有用:

import datetime
import matplotlib.pyplot as plt


def make_plot(item, ap2id):
    ref_data1_pred = res[(res['ITEM'] == item) & (res['AP2ID'] == ap2id) & (res['WEEK'] >= '201701')]['QTY_PRED']
    ref_data1_real = res[(res['ITEM'] == item) & (res['AP2ID'] == ap2id) & (res['WEEK'] >= '201701')]['QTY_AP2']
    ref_data1_x = res[(res['ITEM'] == item) & (res['AP2ID'] == ap2id) & (res['WEEK'] >= '201701')]['WEEK']
    plt.figure()
    plt.plot(ref_data1_x, ref_data1_pred, 'r')
    plt.plot(ref_data1_x, ref_data1_real, 'b')
    plt.xticks(rotation=70)
    plt.xlabel("week")
    plt.ylabel("Qty")
    plt.title(item + " - " + ap2id)
#     plt.savefig("../image/qty/Graph_" + str(datetime.datetime.now()) +"_" + ap2id +".png", format="PNG")
    plt.show()


res = pd.DataFrame(columns=['WEEK', 'ITEM', 'AP2ID', 'QTY_AP2', 'QTY_PRED'])
res.loc[1]=['201801', 'a', '1', 5, 2]
res.loc[2]=['201802', 'a', '1', 8, 5]
res.loc[3]=['201803', 'a', '1', 1, 6]
res.loc[4]=['201804', 'a', '1', 0, 2]
res.loc[5]=['201805', 'a', '1', 4, 3]

for item in res['ITEM'].drop_duplicates():
    for ap2id in res['AP2ID'].drop_duplicates():
        make_plot(item, ap2id)

并生成以下图:

enter image description here

这是您想要的吗?