0

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)   

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

How stupid of me... Works great using your additions. Thanks for the help!
0

You don't ever actually use your DateFormatter.

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.