0

Here's the code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from tkinter import ttk

# Read data from Excel file
df = pd.read_excel('Timesheet.xlsx')

# Create the main window
root = Tk()
root.title("Analog Clocks")
root.geometry("800x600")

# Additional Variables
start_time_a = '00:00:00'
end_time_a = '11:59:59'
start_time_b = '12:00:00'
end_time_b = '23:59:59'

# Function to update the analog clock based on the selected date and time interval
def update_clock(selected_date, start_time, end_time):
...
...

# Create a new figure for the analog clock
    fig = plt.figure(figsize=(7, 7))
    ax = plt.subplot(111, projection='polar')
    ax.set_theta_direction(-1)
    ax.set_theta_offset(np.pi/2)
    ax.set_xticks(np.linspace(0, 2*np.pi, 12, endpoint=False))
    ticks = ['12:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00']
    ax.set_xticklabels(ticks)
    ax.tick_params(axis='x', colors='black')
    plt.setp(ax.get_yticklabels(), visible=False)
    plt.ylim(0, 1)

    # Plot time intervals for case A on the analog clock (red color)
    for index, row in filtered_df_a.iterrows():
        start_time = pd.to_datetime(row['ON'], format='%H:%M:%S')
        end_time = pd.to_datetime(row['OFF'], format='%H:%M:%S')
        
        if start_time <= pd.to_datetime('11:59:59', format='%H:%M:%S') and end_time > pd.to_datetime('11:59:59', format='%H:%M:%S'):
            end_time_a = pd.to_datetime('11:59:59', format='%H:%M:%S')
            start_time_b = pd.to_datetime('12:00:00', format='%H:%M:%S')
            end_time = end_time_a
        else:
            end_time_a = end_time
            start_time_b = pd.to_datetime('12:00:00', format='%H:%M:%S')
            end_time_b = end_time
                
        duration = end_time - start_time
        start_angle = 2 * np.pi * (start_time.hour + start_time.minute / 60 + start_time.second / 3600) / 12
        end_angle = 2 * np.pi * (end_time.hour + end_time.minute / 60 + end_time.second / 3600) / 12

        # Calculate the number of seconds in the interval
        num_seconds = duration.total_seconds()

        # Plot narrow lines for each second in the interval (case A - red color)
        for i in range(int(num_seconds)):
            angle = start_angle + (end_angle - start_angle) * i / num_seconds
            ax.plot([angle, angle], [0, 1], color='red', linewidth=1)

# Display the analog clock
    plt.axis('on')
    plt.show() 

So, I've plot time intervals on a "clock" polar chart, separated by 12 hours (00:00 to 11:59 and 12:00 to 23:59) but I can't figure out how to plot that time intervals that begins before 12:00 and ends after that (e.g 11:58:38 - 12:10:17). I've tried to consider end time as 11:59:59 and calculate duration = end time - start time and plot accordingly but in vain (code in italics). All other time intervals are plotted but the time intervals from AM to PM or viceversa don't appear on the plot.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Dacos
  • 1
  • 1
  • 1
    Welcome back to Stack Overflow! Please take the [tour]. SO is a Q&A site, but there's no question here, and it's hard to tell what exactly you need help with. Check out [ask] and [edit] to clarify. You should also make a [mre], meaning minimal, working code, some example input data, and desired output, as well as current output for illustration. For more explanation, see [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/q/284236/4518341) – wjandrea Jun 05 '23 at 18:03

0 Answers0