I have a small GUI application using tkinter, matplotlib and numpy. Here user will upload an excel file and get multi line graph. But the main problem is I couldn't put the measures on x-axis. Here date and time combination will be the measure of x-axis. But only year is coming as measure. Here I am sharing my code:
import tkinter as tk
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import pandas as pd
import matplotlib.dates
import numpy as np
from datetime import datetime
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 1000, height = 300)
canvas1.pack()
label1 = tk.Label(root, text='Data Analyser')
label1.config(font=('Arial', 20))
canvas1.create_window(400, 50, window=label1)
def getExcel1 ():
global df
import_file_path = filedialog.askopenfilename()
df = pd.read_excel (import_file_path)
daytime=df.apply(lambda r : pd.datetime.combine(r['Day'],r['Time']),1)
global bar1
x = daytime
y1 = df['Count']
y2 = df['Month']
figure1 = Figure(figsize=(8,3), dpi=100)
subplot1 = figure1.add_subplot(111)
subplot2 = figure1.add_subplot(111)
bar1 = FigureCanvasTkAgg(figure1, root)
bar1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
subplot1.plot(x, y1, color='green', linestyle='solid', linewidth = 2, marker='o',
markerfacecolor='green', markersize=8, label='y1')
subplot2.plot(x, y2, color='red', linestyle='solid', linewidth = 2, marker='o',
markerfacecolor='red', markersize=8, label='y2')
def clear_charts():
bar1.get_tk_widget().pack_forget()
browseButton_Excel1 = tk.Button(text='Load File...', command=getExcel1, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(400, 180, window=browseButton_Excel1)
button2 = tk.Button (root, text='Clear Chart', command=clear_charts, bg='green', font=('helvetica', 11, 'bold'))
canvas1.create_window(400, 220, window=button2)
button3 = tk.Button (root, text='Exit!', command=root.destroy, bg='green', font=('helvetica', 11, 'bold'))
canvas1.create_window(400, 260, window=button3)
root.mainloop()
The measure of x-axis should look like '2021-09-06 16:35:00', but it is only comes with '2021'. Here is my excel file date:
And also please suggest me how to put legend, axis label and figure name. Please help me out for this problem.
CodePudding user response:
It's because it's issue in lambda
functon. Simply concat with plus operator instead
import tkinter as tk
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import pandas as pd
import matplotlib.dates
import numpy as np
from datetime import datetime
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 1000, height = 300)
canvas1.pack()
label1 = tk.Label(root, text='Data Analyser')
label1.config(font=('Arial', 20))
canvas1.create_window(400, 50, window=label1)
def getExcel1 ():
global df
import_file_path = filedialog.askopenfilename()
df = pd.read_excel (import_file_path)
df['Day'] = pd.to_datetime(df['Day'])
daytime=df['Day'].astype(str) ' ' df['Time'].astype(str)
global bar1
x = daytime
y1 = df['Count']
y2 = df['Month']
figure1 = Figure(figsize=(8,3), dpi=100)
subplot1 = figure1.add_subplot(111)
subplot2 = figure1.add_subplot(111)
bar1 = FigureCanvasTkAgg(figure1, root)
bar1.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
subplot1.plot(x, y1, color='green', linestyle='solid', linewidth = 2, marker='o',
markerfacecolor='green', markersize=8, label='y1')
subplot2.plot(x, y2, color='red', linestyle='solid', linewidth = 2, marker='o',
markerfacecolor='red', markersize=8, label='y2')
def clear_charts():
bar1.get_tk_widget().pack_forget()
browseButton_Excel1 = tk.Button(text='Load File...', command=getExcel1, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(400, 180, window=browseButton_Excel1)
button2 = tk.Button (root, text='Clear Chart', command=clear_charts, bg='green', font=('helvetica', 11, 'bold'))
canvas1.create_window(400, 220, window=button2)
button3 = tk.Button (root, text='Exit!', command=root.destroy, bg='green', font=('helvetica', 11, 'bold'))
canvas1.create_window(400, 260, window=button3)
root.mainloop()
Gives #