DavidG
DavidG

Reputation: 25363

Time formatting in matplotlib

I have some code that plots some random data on the y axis and plots the current time on the y axis.

My question is a simple one but I can't figure out how to do it. The time that I plot on the x axis has the format like this:

enter image description here

My question is, how do I get rid of the last 6 digits? Am I formatting the datetime wrong? I have looked at other questions on here and most appear to not have this problem when using datetime.datetime.now()

My code is below

time_start = datetime.datetime.now()
time_current = time_start                       
dt = time_current - time_start


fig1 = plt.figure(figsize=(6,2))                      
plt.ion()                                       


while dt.seconds < 100:

    DateFormatter('%H.%M.%S')
    x = datetime.datetime.now()


    y1 = np.random.random()*30                  
    y2 = np.random.random()
    y3 = np.random.random()*-10


    plt.plot(x,y1, color='blue', marker = 'o')
    plt.plot(x,y2, color='orange', marker = 'o')
    plt.plot(x,y3, color='grey', marker = 'o')

    plt.gcf().autofmt_xdate()

    plt.xlabel('Time')                                                       
    plt.ylabel('Temperature/degrees')

    plt.pause(2)                                                           


    time_current = datetime.datetime.now()                       
    dt = time_current - time_start

    plt.draw()  

fig1.savefig('foo1.png')
plt.show(block=True)   

Upvotes: 0

Views: 444

Answers (2)

tmdavison
tmdavison

Reputation: 69066

You don't ever actually use your DateFormatter.

Try this:

plt.gca().xaxis.set_major_formatter(DateFormatter('%H.%M.%S'))

Upvotes: 0

xnx
xnx

Reputation: 25478

You're not actually using the DateFormatter you create. Try:

date_formatter = DateFormatter('%H.%M.%S')
ax = plt.gca()
ax.xaxis.set_major_formatter(date_formatter)

Upvotes: 2

Related Questions