ミリ秒をサポートするために上記の回答を改善します。
import numpy as np
import matplotlib.pyplot as plt
from time import mktime
from datetime import datetime
def main():
ticks = [ "2013-9-28 11:00:00.234",
"2013-9-28 11:00:00.334",
"2013-9-28 11:00:00.434",
"2013-9-28 11:00:00.534",
"2013-9-28 11:00:00.634"]
y = np.array([10, 12, 9, 15, 11])
fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot(1,1,1)
dtlst = str2dt(ticks)
ax.stem(dt2msec(dtlst),y)
bins = 5
dtlst = list(linspace(dtlst[0],dtlst[-1],bins))
ax.set_xticks(dt2msec(dtlst))
ax.set_xticklabels(dt2str(dtlst),rotation=15,ha='right')
ax.yaxis.grid(True)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
return
def str2dt(strlst):
year = ""#"2014-"
dtlst = [datetime.strptime(year+i, "%Y-%m-%d %H:%M:%S.%f") for i in strlst]
return dtlst
def dt2msec(dtlst):
floatlst = [mktime(dt.timetuple())*1000+dt.microsecond/1000 for dt in dtlst]
return floatlst
def dt2str(dtlst):
dtstr = [dt.strftime("%Y-%m-%d %H:%M:%S.%f %Z%z") for dt in dtlst]
return dtstr
def msec2dt(msecs):
dtlst = [datetime.fromtimestamp(msecs/1000.0).replace(microsecond=msecs%1000*1000) for msecs in floatlst]
return dtlst
def linspace(start, end, bins):
delta = (end - start)/bins
x = start
while x <= end:
yield x
x = x + delta
return
#-----------------------------------------#
if __name__ == "__main__":
main()
#-----------------------------------------#
出力: