plot time of day vs date in matplotlib

前端 未结 2 1376
一整个雨季
一整个雨季 2020-12-19 08:48

I want to plot, in a specific few ways, dates on the x axis and time of day on the y axis, and then have either line plots or interval (floating bar) plots.

This S

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 09:44

    Follow Joe Kingston's response, but I'd add that you need to convert your date strings into datetime objects, which can easily be done with the datetime module, and then convert them to matplotlib dates represented as floats (which can then be formatted as desired).

    import datetime as dt
    import matplotlib as mpl
    
    some_time_str = '2010-12-20 05:00:00'
    some_time_dt = dt.datetime.strptime(some_time_str, '%Y-%m-%d %H:%M:%S')
    some_time_num = mpl.dates.date2num(some_time_dt) # 734126.20833333337
    

    If you start with an array filled with time strings, you can do something like:

    time_str_list = ['2010-12-20 05:00:00','2010-12-20 05:30:00']
    time_num_list = map(
        lambda x: mpl.dates.date2num(dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')),
        time_str_list) #[734126.20833333337, 734126.22916666663]
    

提交回复
热议问题