You might find these interesting.
https://matplotlib.org/stable/gallery/ti...rrule.html
https://matplotlib.org/stable/gallery/ti...ators.html
https://matplotlib.org/stable/gallery/ti...nvert.html
A comment about functions.
Do not use global variables to return values from a function. Instead of this:
def GetFile():
from tkinter import filedialog
filename = filedialog.askopenfilename(initialdir = "C:/users/me/Desktop/format ticks/",
title = "Select a File",
filetypes = (("Excel files",
"*.xlsx*"),
("all files",
"*.*")))
global pathTo
pathTo=str(filename)
print(pathTo)
global df
df=pd.read_excel(pathTo, 'Sheet1' )
Do this:
def GetFile():
filename = filedialog.askopenfilename(
initialdir = "C:/users/me/Desktop/format ticks/",
title = "Select a File",
filetypes = (("Excel files", "*.xlsx*"), ("all files", "*.*")))
return pd.read_excel(filename, 'Sheet1') , str(filename) # Return the values you want to export
The caller would get the returned values like this:
df, path = GetFile()
And you would pass these to functions arguments instead of having the function use the global variable.
LastT(path) # Pass argument instead of using global variable
...
def LastT(path): # Come up with a better function name
ws1 = load_workbook(path)['Sheet1']
for row in range(3, ws1.max_row):
if(ws1.cell(row,3).value is None):
break
l1=Label(text="Temp "+str(ws1.cell(row,3).value)) # Do not draw over labels, change their text instead.
l1.place(x=40,y=50) # Avoid using place. Use layout managers pack() or grid() instead.
plt.show()
Another option is use classes. I prefer using classes when writing GUI applications. The example below subclasses tk.Tk() and makes a root level window. The class creates the controls and manages data used by the window. The class also has functions that are used by the window. This example plots temperature data (random numbers) 4 times a second. The plot labels use a recurrence rule to place a tick every second.
import matplotlib.pyplot as plt
from matplotlib.dates import (SECONDLY, DateFormatter, rrulewrapper, RRuleLocator)
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import tkinter as tk
from datetime import datetime, timedelta
import random
class MyWindow(tk.Tk):
def __init__(self, *args, count=20, delay=250, **kwargs):
super().__init__(*args, **kwargs)
self.title("Temperature")
self.update_event = None
self.update_delay = delay
self.num_points = count
# Create some initial data
delta = timedelta(milliseconds=self.update_delay)
start = datetime.now() - delta * count
self.time = [start + delta * i for i in range(count)]
self.temperature = np.array([20.0] * count)
# Create a plot to display the data
fig, self.axis = plt.subplots()
self.axis.xaxis.set_tick_params(rotation=30)
self.axis.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
self.axis.xaxis.set_major_locator(RRuleLocator(rrulewrapper(SECONDLY, interval=1)))
self.canvas = FigureCanvasTkAgg(fig, master=self)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.line, = self.axis.plot(self.time, self.temperature)
# Create some buttons to start/stop the display or quit the application
button = tk.Button(self, text="Start", command=self.update_loop)
button.pack(padx=5, pady=5, side=tk.LEFT, fill=tk.X, expand=1)
button = tk.Button(self, text="Stop", command=self.stop_update)
button.pack(padx=5, pady=5, side=tk.LEFT, fill=tk.X, expand=1)