在没有时区的 UTC 中创建可以由 Matplotlib 导入的日期时间对象

Creating datetime objects in UTC without timezone that can be imported by Matplotlib

我有一个以这种方式创建的 datetime 个对象的列表:

from datetime import datetime
import pytz

_year = 2018
_month = 2
_day = 3
_hour = 14
_minute = 30
csv_timezone = pytz.timezone('Europe/Berlin')

csv_dt = datetime(_year, _month, _day, _hour, _minute)
print('csv_dt')
print(csv_dt)

稍后我想在 UTC 中为 Matplotlib 使用:

utc_dt = csv_timezone.localize(csv_dt).astimezone(pytz.utc)
print('utc_dt')
print(utc_dt)

结果如下:

csv_dt
2018-02-03 14:30:00
utc_dt
2018-02-03 13:30:00+00:00

如前所述,我想在 Matplotlib 中使用这些对象。根据文档,其中需要以下日期时间对象:

Date formatting Commonly, in Python programs, dates are represented as datetime objects, so we have to first convert other data values into datetime objects, sometimes by using the dateutil companion module, for example:

import datetime

date = datetime.datetime(2009, 03, 28, 11, 34, 59, 12345)

or

import dateutil.parser

datestrings = ['2008-07-18 14:36:53.494013','2008-07-2014:37:01.508990', '2008-07-28 14:49:26.183256']

dates = [dateutil.parser.parse(s) for s in datestrings]

Once we have the datetime objects, in order to let Matplotlib use them, we have to convert them into floating point numbers that represent the number of days since 0001-01-01 00:00:00 UTC.

To do that, Matplotlib itself provides several helper functions contained in the matplotlib.dates module:

• date2num(): This function converts one or a sequence of datetime objects to float values representing days since 0001-01-01 00:00:00 UTC (the fractional parts represent hours, minutes, and seconds)

(Excerpt from Matplotlib for Python Developers, Sandro Tosi, Ed. PACKT PUBLISHING 2009. Page 95)

所以我不明白为什么 datetime 对象 date2num 函数期望具有以下形式:

2008-07-20 14:37:01.508990

虽然我生成的格式是这样的:

2018-02-03 13:30:00+00:00

我得到的错误是:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-7d0a84cb48da> in <module>
     20 
     21 # Convert to matplotlib required
---> 22 mpl_times =  mpl.dates.date2num(times)
     23 
     24 plt.figure()

times 是:

<class 'numpy.ndarray'>
times
['2018-01-12 07:00:00+01:00', '2018-01-12 07:01:00+01:00', '2018-01-12 07:02:00+01:00' ..... ]

如何将我的格式转换为 date2num 期望的格式?

如何导入 matplotlib.dates? 你能试试吗

    from matplotlib import dates as dt
    ....
    ....
    mpl_times =  dt.date2num(times)

下面的代码适合我

    from datetime import datetime
    import pytz
    from matplotlib import dates as dt

    _year = 2018
    _month = 2
    _day = 3
    _hour = 14
    _minute = 30
    csv_timezone = pytz.timezone('Europe/Berlin')

    csv_dt = datetime(_year, _month, _day, _hour, _minute)
    print('csv_dt')
    print(csv_dt)

    utc_dt = csv_timezone.localize(csv_dt).astimezone(pytz.utc)
    print('utc_dt')
    print(utc_dt)
    print (dt.date2num(utc_dt))

输出为736728.5625