Python Forum
MatplotibTicks every 24 hours
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MatplotibTicks every 24 hours
#1
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()
Yoriz write Sep-18-2022, 11:37 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
   
Reply
#3
when using tkinter, you can use 'after' to interrupt a process
see: https://stackoverflow.com/a/25753719
Reply
#4
(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
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hello all! need help. calculating OT hours. no1up 5 1,499 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,840 Jun-30-2021, 05:14 PM
Last Post: Larz60+
  How to get utcnow + 2 hours? korenron 2 2,551 Mar-01-2021, 03:22 PM
Last Post: korenron
  Need to add hours min and seconds tester_V 5 3,115 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