简体   繁体   中英

How to plot time using pyplot?

I have a csv with two columns, one with week day names and one with time of login of the employee. How do I plot this using matplotlib or pyplot? not able to get how to plot the time in Y coordinate. the time data is like "23/02/2017 at 11:30 PM".

Do I need to make changes to the data or Can I plot it as is?

Format the time using datetime, and then plot the formatted date directly using pyplot. An example code.

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

sampleTime = "23/02/2017 at 11:30 PM"
timeFormatted = datetime.strptime(sampleTime, "%d/%m/%Y at %I:%M %p")
print(timeFormatted)

plt.scatter(timeFormatted, 0)

# reshape X labels
plt.gcf().autofmt_xdate()
showFormat = mdates.DateFormatter('%Y-%m-%d %H:%M')
plt.gca().xaxis.set_major_formatter(showFormat)

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM