matplotlib字符串到日期

Try*_*ard 1 python matplotlib

嗨我想在matplotlib中将日期列表转换为字符串到x轴,我似乎无法让它出来正确.

dates =  ['2014-05-06', '2014-05-07', '2014-05-08', '2014-05-09', '2014-05-10', '2014-05-11', '2014-05-12', '2014-05-13']

import matplotlib
from matplotlib import pyplot
from matplotlib import dates

converted_dates = matplotlib.dates.datestr2num(dates)
x_axis = (converted_dates)

y_axis = range(0,8)
pyplot.plot( x_axis, y_axis, '-' )
pyplot.show()
Run Code Online (Sandbox Code Playgroud)

这会在图表的x轴上带回1 2 3 4 5 6 7,我错过了什么.我希望这能显示2014-05-06等

cph*_*wis 6

这是目标吗?(投入轮换,因为它几乎总是出现,有日期.)

datelist =  ['2014-05-06', '2014-05-07', '2014-05-08', '2014-05-09', '2014-05-10',    '2014-05-11', '2014-05-12', '2014-05-13']

import matplotlib
from matplotlib import pyplot
from matplotlib import dates
import datetime

converted_dates = list(map(datetime.datetime.strptime, datelist, len(datelist)*['%Y-%m-%d']))
x_axis = converted_dates
formatter = dates.DateFormatter('%Y-%m-%d')


y_axis = range(0,8)
pyplot.plot( x_axis, y_axis, '-' )
ax = pyplot.gcf().axes[0] 
ax.xaxis.set_major_formatter(formatter)
pyplot.gcf().autofmt_xdate(rotation=25)
pyplot.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述