pandas plot with timestamps on x axis - tick units?

前端 未结 1 613
無奈伤痛
無奈伤痛 2021-01-26 06:52

I have a pandas data frame with datetime values:

    Date/Time           Event Value
0   2018-11-15 17:17:49 62
1   2018-11-15 17:27:50 63
2   2018-11-15 17:37:5         


        
1条回答
  •  迷失自我
    2021-01-26 07:08

    If you plot a dataframe with the x_compat=True option you can be certain that the units are matplotlib.dates units. Else, pandas may decide some units for you.

    Here, pandas uses matplotlib dates units. They are defined as:

    Matplotlib represents dates using floating point numbers specifying the number of days since 0001-01-01 UTC, plus 1. For example, 0001-01-01, 06:00 is 1.25, not 0.25. Values < 1, i.e. dates before 0001-01-01 UTC are not supported.

    You are however not expected to calculate those units yourself. Matplotlib provides helper functions

    • matplotlib.dates.date2num
    • matplotlib.dates.num2date
    • matplotlib.dates.datestr2num

    which you can use to calculate back and forth between the units.

    As an example, matplotlib.dates.datestr2num("2018-11-15 19:27:48") gives you 737013.8109722222.

    0 讨论(0)
提交回复
热议问题