Python Forum
MatplotibTicks every 24 hours - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: MatplotibTicks every 24 hours (/thread-38221.html)



MatplotibTicks every 24 hours - TamP - Sep-18-2022

My code displays temperatures imported from an excel sheet.
The graph works as I want but I cannot see how to show an xtick every 24 hours rather than the hourly ticks it is currently showing
This beginner to matplotlib seeks advice
Thanks in advance
    plt.figure(figsize=(24, 16))
    plt.xticks(fontsize=15,fontweight='bold')
    plt.yticks(fontsize=15,fontweight='bold')
    plt.plot(df[str('Temperature')],ls='-',linewidth=10)
    plt.plot(df[str('Zero')], ls='-')
    plt.xlabel('Hours since start',fontsize=15,fontweight='bold')
    plt.ylabel('temp',fontsize=15,fontweight='bold')
    plt.title('Temperature Centigrade',fontsize=15,fontweight='bold')
    plt.grid(True)
    plt.gca().xaxis.set_major_locator(mdates.DayLocator()) 
    plt.gcf().autofmt_xdate()
    plt.xticks(ticks=None, labels=None, rotation=90) 
    plt.show()



RE: MatplotibTicks every 24 hours - TamP - Oct-02-2022

I have two python scripts. One gathers Time, Date and temperature from a BME280 sensor on a raspberry Pi.
The second script displays a chart of the date time and temperature.
However it defaults to hours as the major ticks.
How do I get it to use the date as the major X ticks
Any help appreciated

import matplotlib.pyplot as plt 
import pandas as pd
import openpyxl
from tkinter import *
from tkinter import messagebox
root=Tk()
root.title("A Chart Program")
# size of window
root.geometry('350x150')
#Centre the Window
root.eval('tk::PlaceWindow . center')

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' )

def LastT():
    from openpyxl import Workbook
    from openpyxl import load_workbook    
    wb = load_workbook(pathTo)
    ws1 = wb['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))
    l1.place(x=40,y=50)
    plt.show()
    

def GetTime():
    path = pathTo
    wb_obj = openpyxl.load_workbook(path) 
    sheet_obj = wb_obj.active 
    cell_obj = sheet_obj.cell(row = 2, column = 2)
    startTime=(str((cell_obj.value)))
    messagebox.showinfo("AlbaSoft","Start Time was "+str(startTime[0:8]))
    
def GetDate():
    path = pathTo
    wb_obj = openpyxl.load_workbook(path) 
    sheet_obj = wb_obj.active 
    cell_obj = sheet_obj.cell(row = 2, column = 1)
    startDate=(str((cell_obj.value)))
    messagebox.showinfo("MySoft","Start date was "+str(startDate[0:10]))
    

def But1():
    plt.figure(figsize=(24, 16))
    plt.xticks(fontsize=15,fontweight='bold')
    plt.yticks(fontsize=15,fontweight='bold')
    plt.plot(df[str('Temperature')],ls='-',linewidth=10)
    plt.plot(df[str('Zero')], ls='-')
    plt.xlabel('Hours since start',fontsize=15,fontweight='bold')
    plt.ylabel('temp',fontsize=15,fontweight='bold')
    plt.title('Temperature Centigrade',fontsize=15,fontweight='bold')
    plt.grid(True)
    plt.show()

    
#QUIT program
def close_window(): 
    root.destroy()

#Place the Buttons
b1=Button(text="Temperature",command=But1)#react to button press
b1.place(x=30,y=10)#Position button


#QUIT BUTTON
bQuit=Button(text="Quit",command=close_window)
bQuit.place(x=160,y=100)

GetFile()
LastT()

root.mainloop
[attachment=2037]


RE: MatplotibTicks every 24 hours - Larz60+ - Oct-02-2022

when using tkinter, you can use 'after' to interrupt a process
see: https://stackoverflow.com/a/25753719


RE: MatplotibTicks every 24 hours - TamP - Oct-03-2022

(Oct-02-2022, 04:31 PM)Larz60+ Wrote: when using tkinter, you can use 'after' to interrupt a process
see: https://stackoverflow.com/a/25753719

Yes but that does not help my ticks problem


RE: MatplotibTicks every 24 hours - deanhystad - Oct-04-2022

You might find these interesting.

https://matplotlib.org/stable/gallery/ticks/date_demo_rrule.html
https://matplotlib.org/stable/gallery/ticks/date_formatters_locators.html
https://matplotlib.org/stable/gallery/ticks/date_demo_convert.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)