我有这段代码,可以按特定物料编号分析哪些交货延迟,或者可以显示所有物料编号哪些延迟交货。在这里,我按“早交货”,“准时交货”和“晚交货”对“延迟”进行了分类,然后想对这些结果进行绘图。这段代码在没有GUI应用程序部分的情况下工作得很好(tkinter),现在,当我尝试生成图形时,想要指定是否要包含所有材料,而不是特定材料,单击按钮时什么也没有发生执行我的代码。我相信这与单选按钮以及如何在if语句中设置最终生成图形的值有关;关于我应该如何处理该部分代码的逻辑的任何建议?我希望该应用程序能够生成我列出的类别的条形图。
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tkinter import*
master = Tk()
label2= Label(master, text='Start Year')
label2.grid(row=4, column=0)
label3= Label(master, text='End Year')
label3.grid(row=5, column=0)
label4= Label(master, text='Material')
label4.grid(row=6, column=0)
textBox = Text(master, height=1, width=10, font=('Consolas', 9))
textBox.grid(row=4, column=1)
textBox2 = Text(master, height=1, width=10, font=('Consolas', 9))
textBox2.grid(row=5, column=1)
textBox3 = Text(master, height=1, width=10, font=('Consolas', 9))
textBox3.grid(row=6, column=1)
All = StringVar()
All.set(1)
Alll = Radiobutton(master, text="All", variable=All, value=2)
Alll.grid(row=7, column=0)
Material = Radiobutton(master, text="Material", variable=All, value=1)
Material.grid(row=8, column=0)
def retrieve_input():
StartYear = textBox.get("1.0","end-1c")
EndYear = textBox2.get("1.0","end-1c")
Materiall = textBox3.get("1.0","end-1c")
Material= 'Material'
DELIVERY_DATE = 'Delivery Date'
DESIRED_DATE = 'source desired delivery date'
DELAYED_DAYS = 'Delayed Days'
df = pd.read_csv('otdo.csv')
df['Delivery Date'] = pd.to_datetime(df['Delivery Date'], format='%m/%d/%Y')
df['source desired delivery date'] = pd.to_datetime(df['source desired delivery date'], format='%m/%d/%Y')
late_threshold = pd.Timedelta(days=0)
late_threshold2 = pd.Timedelta(days=10)
df[DELIVERY_DATE] = pd.to_datetime(df[DELIVERY_DATE])
df[DESIRED_DATE] = pd.to_datetime(df[DESIRED_DATE])
df[DELAYED_DAYS] = df[DELIVERY_DATE] - df[DESIRED_DATE]
df2 = df[(df['Delivery Date'].dt.year >= int(StartYear)) & (df['Delivery Date'].dt.year <= int(EndYear))]
df3 = df2[ df2[DELAYED_DAYS] > late_threshold]
df3 = df3[late_threshold2 > df3[DELAYED_DAYS]]
df3 = df3.loc[df['Material'].str.contains('1005459', na=False)]
df4 = df2[ df2[DELAYED_DAYS] > late_threshold2]
df4 = df4.loc[df['Material'].str.contains('1005459', na=False)]
df5 = df2[df2[DELAYED_DAYS] <= late_threshold]
df5 = df5.loc[df['Material'].str.contains('1005459', na=False)]
df6 = df2.loc[df['Material'].str.contains('1005459', na=False)]
df7 = df2[ df2[DELAYED_DAYS] > late_threshold]
df7 = df7[late_threshold2 > df7[DELAYED_DAYS]]
df8 = df2[ df2[DELAYED_DAYS] > late_threshold2]
df9 = df2[df2[DELAYED_DAYS] <= late_threshold]
zero3 = df4.count()
zero4 = df5.count()
zero5 = df7.count()
zero6 = df8.count()
zero7 = df9.count()
if All == 1:
objects = ('1', '2', '3')
y_pos = np.arange(len(objects))
values = [zero3.Material, zero4.Material, zero5.Material]
plt.bar(y_pos, values, align='center', alpha=0.2)
plt.xticks(y_pos, objects)
plt.show()
if All == 2:
objects = ('1', '2', '3')
y_pos = np.arange(len(objects))
values = [zero5.Material, zero6.Material, zero7.Material]
plt.bar(y_pos, values, align='center', alpha=0.2)
plt.xticks(y_pos, objects)
plt.show()
button1 = Button(master,text="Show Values", command=lambda: retrieve_input())
button1.grid(row=13, column=1)
mainloop( )
csv文件:
Gui:
生成的图形(没有tkinter部分)
样本数据:
Material Delivery Date source desired delivery date
3334678 12/31/2014 12/31/2014
233433 12/31/2014 12/31/2014
3434343 1/5/2015 1/6/2015
3334567 1/5/2015 1/5/2015
546456 2/11/2015 2/10/2015
221295 4/10/2015 4/10/2015
20065316 2/17/2015 2/17/2015
10011155 1/5/2015 1/5/2015