Matplotlib中的日期时间是否与Pandas或Python3中使用的日期时间不同?是否需要使用date2num
之类的Matplotlib函数进行显式转换?
背景:
matplotlib.dates说它使用0001-01-01 UTC
中的浮点。
Matplotlib使用浮点数表示日期,该数字指定 自0001-01-01 UTC以来的天数,再加上1。例如, 0001-01-01、06:00是1.25,而不是0.25。值<1,即之前的日期 不支持0001-01-01 UTC。
Python Data Visualization with Matplotlib说:
我们需要将值转换为Matplotlib支持的浮点数。
from matplotlib.dates import date2num
import pandas as pd
# Convert Date column from string to Python datetime object,
# then to float number that is supported by Matplotlib.
stock_df["Datetime"] = date2num(pd.to_datetime(stock_df["Date"], format="%Y-%m-%d").tolist())