matplotlib 中月-日-年图所需的格式
Required format for month-day-year plot in matplotlib
我有一个 .csv 文件,其中一列包含日期,使用反斜杠“/”在一列中分隔月、日和年。以下三列包含平均财务价值、较高财务价值和较低财务价值。数据格式如下例所示;
3/2/14 18338.98 18734.07 17943.88
3/3/14 18280.09 18675.2 17884.99
3/4/14 18220.26 18614.53 17825.98
3/5/14 18160.29 18551.71 17768.87
如何让 python 读取日期列并将其识别为月、日、年格式的日期,以便最终使用 matlibplot 绘图?
使用datetime
模块创建datetime
对象
>>> import datetime
>>> dt = datetime.datetime.strptime('3/5/14', '%m/%d/%y')
>>> dt
datetime.datetime(2014, 3, 5, 0, 0)
使用 strftime
方法为轴 ticks
创建标签。
>>> dt.strftime('%d%b%Y')
'05Mar2014'
>>>
我有一个 .csv 文件,其中一列包含日期,使用反斜杠“/”在一列中分隔月、日和年。以下三列包含平均财务价值、较高财务价值和较低财务价值。数据格式如下例所示;
3/2/14 18338.98 18734.07 17943.88
3/3/14 18280.09 18675.2 17884.99
3/4/14 18220.26 18614.53 17825.98
3/5/14 18160.29 18551.71 17768.87
如何让 python 读取日期列并将其识别为月、日、年格式的日期,以便最终使用 matlibplot 绘图?
使用datetime
模块创建datetime
对象
>>> import datetime
>>> dt = datetime.datetime.strptime('3/5/14', '%m/%d/%y')
>>> dt
datetime.datetime(2014, 3, 5, 0, 0)
使用 strftime
方法为轴 ticks
创建标签。
>>> dt.strftime('%d%b%Y')
'05Mar2014'
>>>