DateFormatter %H:%M:%S override when using graph update function

Question summary

When using an update function, hour,minutes and seconds magically disappear from the x-axis scale while year, month and day are still present. I have 2 code samples. In the first sample the hour,minutes and seconds notation is displayed perfectly on the x-axes.
In the second code sample is using an update function. In this code hour, minute and seconds are set to 00:00:00 on the x-axis.
Format should be correct. I first thought that this had something to do with the data.
But the graph is displayed correctly and even the right corner box shows the right data.
The initial code has integer input for date like: 2021,05,19,19,45,00. I changed this later to a datetime float array. It doesn’t matter. It has no effect. The issue is likely some initialization issue that is present when using the update function.

H,M,S gets overridden but how to prevent?
It drives me insane. Please help me.

Conditions and versions

Using OS Ubuntu 18.04
Python: 3.6.9
Matplotlib: 3.3.3
Matplotlib backend: Qt5Agg

Code sample 1 for reproduction: H:M:S present

#!/usr/bin/python3
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime

origin = ['2020-02-05 17:17:55','2020-02-05 17:17:51','2020-02-05 17:17:49']

a = [datetime.strptime(d, '%Y-%m-%d %H:%M:%S') for d in origin]
y = ['35.764299', '20.3008', '36.94704']
x = matplotlib.dates.date2num(a)

figure = plt.figure()
axes = figure.add_subplot(1, 1, 1)

formatter = matplotlib.dates.DateFormatter('%H:%M:%S')  # displayed correctly
axes.xaxis.set_major_formatter(formatter)

plt.setp(axes.get_xticklabels(), rotation = 15)

axes.plot(x, y)
plt.show()

Code sample 2 for reproduction H:M:S overridden

#!/usr/bin/python3
# mathplotlib example

import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.animation import FuncAnimation

# timing variables
update_int = 100    # update interval in ms
rep_del = 1000      # delay after each animation run

# np line space does not support datetime objects. Function to create a np line space.
# start, end are date_time objects
def date_linespace(start, end, steps):
    
    delta = (end - start) / steps
    increments = range(0, steps) * np.array([delta]*steps)
    return start + increments

def init():
    
    ax.set_xlim(start_datetime_object, end_datetime_object)
    ax.set_ylim(0, 3)

    return line,    # the comma makes the return value iterable


def update(frame):

    # frame is numpy float64
    y = fake_y_data.pop(0)

    xdata.append(frame)
    ydata.append(y)

    line.set_data(xdata, ydata)

    return line,


# use two sub plots
fig, ax = plt.subplots(figsize=(5, 4))  # size of graphic
xdata, ydata = [], []
fake_y_data = [2,1,0,1,1,3,0,1,2,3,2,3,0,3,3,1,2,0,2,1]   # test data

# format line
line, = plt.plot([], [], '-r')     # - = solid line, r = red. Must be iterable "," after line

# Major ticks every day.
# xlim MUST be set or tick population will explode and consume lots of memory
fmt_day = mdates.DayLocator(interval=1)
ax.xaxis.set_major_locator(fmt_day)
ax.set_xlim(np.datetime64('2019-08-01'), np.datetime64('2019-09-01'))  # set limit for tick population

# Text in the x axis will be displayed in 'YYYY-mm-dd' format.
#ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))  # This would be working
fmt_hour = mdates.DateFormatter('%H:%M:%S')                      # this is NOT working
ax.xaxis.set_major_formatter(fmt_hour)

# create x time series data: start > end in n frames
# init frames. X- coordinates of type: numpy.ndarray
start_datetime_object = datetime.datetime(2019, 8, 8, 23, 11)     # date time in interger format
end_datetime_object = datetime.datetime(2019, 8, 23, 14, 13)
frames = date_linespace(start_datetime_object, end_datetime_object, 10)
frames = mdates.date2num(frames)                                  # convert to float epoch time or seconds are not displayed

# update function
ani = FuncAnimation(fig, update, frames, init_func=init, blit=True, interval=update_int, repeat_delay=rep_del, repeat=False)


plt.title("my test graph")
plt.xlabel("x-axis")
plt.ylabel("y-axis")

# Right corner coordinates box
ax.format_xdata = mdates.DateFormatter('%H:%M:%S')
ax.format_ydata = lambda x: f'kwh{x:.2f}'  # Format x value in kwh
ax.grid(True)

# Rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them.
fig.autofmt_xdate()                                    # will rotate 30 deg to fit
plt.setp(ax.xaxis.get_majorticklabels(), rotation=30)  # rotate manually (not required)

# show the plot and loop
plt.show()

Thanks for moving this here. You are using a day locator, which means the locator chooses the start of the day as the tick values. The start of a day is 00:00:00

Thank you for your quick reply. I understand what your are saying. Yes, the day is starting with 00:00:00, that is what I’m expecting, but the graph shows several days with lots of time in between. I can use the mouse to find the actual time I expect on the x-axes. First Y=0 is 21:23:24. But all x-axes labels show 00:00:00.

This only allows ticks at 00:00:00 for each day. Perhaps try putting fmt_hour = mdates.DateFormatter('%b-%d %H:%M:%S') in there and you will better understand what is going on…

Yep, yep, I got it. I was not aware that I had that many days. Meanwhile I changed the x-axis to display just one day. In the format I dropped minutes and seconds and have just the hours. Now the graph looks pretty decent. Thanks for your help.