matplotlib reset

Hello when I put the stock symbol and hit the ok button it works fine but when hit the ok button again to see another stock data the previous chart window doesn’t delete but the chart becomes two of them and the newly generated chart is below the previous chart. I want the previous chart to be deleted when ok button is pressed and generate the new chart.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>stock = input()
# label
ttk.Label(left_frame, text='From: ').grid(column=0, row=3)
ttk.Label(left_frame, text='To: ').grid(column=0, row=4)
# from
from_cal = DateEntry(left_frame, selectmode='day', year=2020, month=1, day=1)
from_cal.grid(column=1, row=3)
# to
to_cal = DateEntry(left_frame, selectmode='day', year=today.year, month=today.month, day=today.day)
to_cal.grid(column=1, row=4)
def plot_candlestick(ax, df):
try:
df_reset = df.reset_index()
df_reset['Date'] = df_reset['Date'].map(mdates.date2num)
candlestick_ohlc(ax, df_reset.values, width=0.6, colorup='r', colordown='b')
ax.xaxis_date()
ax.grid(True)
except Exception as e:
msg.showerror('test', 'Error: ' + str(e))
def plot_line(ax, df):
try:
for column in df.columns:
if column != 'Date':
ax.plot(df.index, df[column], label=column)
ax.legend()
ax.grid(True)
except Exception as e:
msg.showinfo('test', 'Error: ' + str(e))
def plot_data(symbol, start_date, end_date):
try:
# reading data
df = fdr.DataReader(symbol, start_date, end_date)
fig_candlestick = plt.figure(figsize=(8, 6))
ax_candlestick = fig_candlestick.add_subplot(111)
plot_candlestick(ax_candlestick, df)
fig_line = plt.figure(figsize=(8, 6))
ax_line = fig_line.add_subplot(111)
plot_line(ax_line, df)
# Embedding candlestick chart
canvas_candlestick = FigureCanvasTkAgg(fig_candlestick, master=frame_candlestick)
canvas_candlestick.draw()
canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# Adding navigation toolbar for candlestick chart
toolbar_candlestick = NavigationToolbar2Tk(canvas_candlestick, frame_toolbar_candlestick)
toolbar_candlestick.update()
canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
win.update_idletasks()
win.geometry(f"{win.winfo_reqwidth()+50}x{win.winfo_reqheight()+50}")
except Exception as e:
msg.showerror('test', 'Error: ' + str(e))
def left_frame_ok_func():
global stock
if stock != '':
plot_data(stock, start_date=from_cal.get_date(), end_date=to_cal.get_date())
else:
msg.showerror('test', 'Please imput stock')
# Frame for candlestick chart and toolbar
frame_candlestick = ttk.Frame(win)
frame_candlestick.grid(row=2, column=0, padx=10, pady=10, sticky="ew")
frame_toolbar_candlestick = ttk.Frame(win)
frame_toolbar_candlestick.grid(row=3, column=0, padx=10, pady=10, sticky="ew")
# left frame ok button
left_frame_ok = ttk.Button(left_frame, text='OK', command=left_frame_ok_func)
left_frame_ok.grid(column=0, row=6)
</code>
<code>stock = input() # label ttk.Label(left_frame, text='From: ').grid(column=0, row=3) ttk.Label(left_frame, text='To: ').grid(column=0, row=4) # from from_cal = DateEntry(left_frame, selectmode='day', year=2020, month=1, day=1) from_cal.grid(column=1, row=3) # to to_cal = DateEntry(left_frame, selectmode='day', year=today.year, month=today.month, day=today.day) to_cal.grid(column=1, row=4) def plot_candlestick(ax, df): try: df_reset = df.reset_index() df_reset['Date'] = df_reset['Date'].map(mdates.date2num) candlestick_ohlc(ax, df_reset.values, width=0.6, colorup='r', colordown='b') ax.xaxis_date() ax.grid(True) except Exception as e: msg.showerror('test', 'Error: ' + str(e)) def plot_line(ax, df): try: for column in df.columns: if column != 'Date': ax.plot(df.index, df[column], label=column) ax.legend() ax.grid(True) except Exception as e: msg.showinfo('test', 'Error: ' + str(e)) def plot_data(symbol, start_date, end_date): try: # reading data df = fdr.DataReader(symbol, start_date, end_date) fig_candlestick = plt.figure(figsize=(8, 6)) ax_candlestick = fig_candlestick.add_subplot(111) plot_candlestick(ax_candlestick, df) fig_line = plt.figure(figsize=(8, 6)) ax_line = fig_line.add_subplot(111) plot_line(ax_line, df) # Embedding candlestick chart canvas_candlestick = FigureCanvasTkAgg(fig_candlestick, master=frame_candlestick) canvas_candlestick.draw() canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) # Adding navigation toolbar for candlestick chart toolbar_candlestick = NavigationToolbar2Tk(canvas_candlestick, frame_toolbar_candlestick) toolbar_candlestick.update() canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1) win.update_idletasks() win.geometry(f"{win.winfo_reqwidth()+50}x{win.winfo_reqheight()+50}") except Exception as e: msg.showerror('test', 'Error: ' + str(e)) def left_frame_ok_func(): global stock if stock != '': plot_data(stock, start_date=from_cal.get_date(), end_date=to_cal.get_date()) else: msg.showerror('test', 'Please imput stock') # Frame for candlestick chart and toolbar frame_candlestick = ttk.Frame(win) frame_candlestick.grid(row=2, column=0, padx=10, pady=10, sticky="ew") frame_toolbar_candlestick = ttk.Frame(win) frame_toolbar_candlestick.grid(row=3, column=0, padx=10, pady=10, sticky="ew") # left frame ok button left_frame_ok = ttk.Button(left_frame, text='OK', command=left_frame_ok_func) left_frame_ok.grid(column=0, row=6) </code>
stock = input()

# label
ttk.Label(left_frame, text='From: ').grid(column=0, row=3)
ttk.Label(left_frame, text='To: ').grid(column=0, row=4)

# from
from_cal = DateEntry(left_frame, selectmode='day', year=2020, month=1, day=1)
from_cal.grid(column=1, row=3)

# to
to_cal = DateEntry(left_frame, selectmode='day', year=today.year, month=today.month, day=today.day)
to_cal.grid(column=1, row=4)

def plot_candlestick(ax, df):
    try:
        df_reset = df.reset_index()
        df_reset['Date'] = df_reset['Date'].map(mdates.date2num)
        candlestick_ohlc(ax, df_reset.values, width=0.6, colorup='r', colordown='b')
        ax.xaxis_date()
        ax.grid(True)
    except Exception as e:
        msg.showerror('test', 'Error: ' + str(e))


def plot_line(ax, df):
    try:
        for column in df.columns:
            if column != 'Date':
                ax.plot(df.index, df[column], label=column)
        ax.legend()
        ax.grid(True)
    except Exception as e:
        msg.showinfo('test', 'Error: ' + str(e))


def plot_data(symbol, start_date, end_date):
    try:
        # reading data
        df = fdr.DataReader(symbol, start_date, end_date)
        fig_candlestick = plt.figure(figsize=(8, 6))
        ax_candlestick = fig_candlestick.add_subplot(111)
        plot_candlestick(ax_candlestick, df)

        fig_line = plt.figure(figsize=(8, 6))
        ax_line = fig_line.add_subplot(111)
        plot_line(ax_line, df)

        # Embedding candlestick chart
        canvas_candlestick = FigureCanvasTkAgg(fig_candlestick, master=frame_candlestick)
        canvas_candlestick.draw()
        canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        # Adding navigation toolbar for candlestick chart
        toolbar_candlestick = NavigationToolbar2Tk(canvas_candlestick, frame_toolbar_candlestick)
        toolbar_candlestick.update()
        canvas_candlestick.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        win.update_idletasks()
        win.geometry(f"{win.winfo_reqwidth()+50}x{win.winfo_reqheight()+50}")
    except Exception as e:
        msg.showerror('test', 'Error: ' + str(e))
        

def left_frame_ok_func():
    global stock
    if stock != '':
        plot_data(stock, start_date=from_cal.get_date(), end_date=to_cal.get_date())
    else:
        msg.showerror('test', 'Please imput stock')


# Frame for candlestick chart and toolbar
frame_candlestick = ttk.Frame(win)
frame_candlestick.grid(row=2, column=0, padx=10, pady=10, sticky="ew")

frame_toolbar_candlestick = ttk.Frame(win)
frame_toolbar_candlestick.grid(row=3, column=0, padx=10, pady=10, sticky="ew")

# left frame ok button
left_frame_ok = ttk.Button(left_frame, text='OK', command=left_frame_ok_func)
left_frame_ok.grid(column=0, row=6)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật