I am using matplotlib (Python), I have y values in seconds (like 1000 seconds) and I want to display the ticks of y-axis in HH:MM:SS format. What is the simple solution to do that (with example).
Y is an integer list represents time duration in seconds
Sample code
import matplotlib.pyplot as plt
import numpy as np
y= np.array([1000,2000,4000,1000])
plt.plot(y)
plt.show()
Best regards
CodePudding user response:
You can use the timedelta function from the datetime module.
import numpy as np
from datetime import timedelta
y= np.array([1000,2000,4000,1000])
y_timedelta = [str(timedelta(seconds=int(s))) for s in y]
Result:
['0:16:40', '0:33:20', '1:06:40', '0:16:40']
CodePudding user response:
Thanks a lot Derek, however I have to use yticks and the full code working is as follows:
import numpy as np
from datetime import timedelta
import matplotlib.pyplot as plt
y= np.array([1000,3000,2000,4000])
y_timedelta = [str(timedelta(seconds=int(s))) for s in y]
plt.plot(y)
plt.yticks(y,y_timedelta)
plt.show()
