Python Forum
MatplotibTicks every 24 hours
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MatplotibTicks every 24 hours
#5
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)
TamP likes this post
Reply


Messages In This Thread
MatplotibTicks every 24 hours - by TamP - Sep-18-2022, 10:48 AM
RE: MatplotibTicks every 24 hours - by TamP - Oct-02-2022, 11:12 AM
RE: MatplotibTicks every 24 hours - by Larz60+ - Oct-02-2022, 04:31 PM
RE: MatplotibTicks every 24 hours - by TamP - Oct-03-2022, 08:13 AM
RE: MatplotibTicks every 24 hours - by deanhystad - Oct-04-2022, 04:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Hello all! need help. calculating OT hours. no1up 5 1,536 Jul-30-2022, 10:00 PM
Last Post: rob101
  Py script that triggers ever 3 hours, but only between 9:15 am to 3:30 pm, Mon to Fri Pymod 2 1,864 Jun-30-2021, 05:14 PM
Last Post: Larz60+
  How to get utcnow + 2 hours? korenron 2 2,586 Mar-01-2021, 03:22 PM
Last Post: korenron
  Need to add hours min and seconds tester_V 5 3,146 Jun-02-2020, 05:29 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020