python繪制圖像日期的簡單處理
<pre>
import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib as mpl
生成圖形窗口
fig = plt.figure()
ax = fig.add_subplot(111)
設(shè)置開始時(shí)間
start = datetime.datetime(2015,1,1)
設(shè)置結(jié)束時(shí)間
stop = datetime.datetime(2016,1,1)
設(shè)置時(shí)間間隔
delta = datetime.timedelta(days=1)
生成橫坐標(biāo)時(shí)間的數(shù)據(jù),dates為array數(shù)組,浮點(diǎn)數(shù)
dates = mpl.dates.drange(start,stop,delta)
生成上下波動的隨機(jī)數(shù)據(jù)
y=np.random.rand(len(dates))
得到當(dāng)前的坐標(biāo)軸對象
ax =plt.gca()
按日期繪制圖形
ax.plot_date(dates,y,'y-')
設(shè)置日期顯示格式
date_format = mpl.dates.DateFormatter('%Y-%m')
將該日期格式設(shè)置設(shè)置到坐標(biāo)系中
ax.xaxis.set_major_formatter(date_format)
自動調(diào)整日期顯示位置
fig.autofmt_xdate()
顯示圖形
plt.show()
</pre>
figure_1.png