python 画图,一个时间数据画图,设置时间标签的小例子

#-*- coding: utf-8 -*- 
#---------------------------------------------------


#---------------------------------------------------
import datetime
from pylab import *
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import matplotlib.dates as mdate
import matplotlib.pyplot as plt
#---------------------------------------------------
now =datetime.datetime.now()
days=datetime.timedelta(10)#10天
ax = gca()
grid(True)#显示网格
ax.xaxis.set_major_formatter(mdate.DateFormatter('%y-%m-%d'))#设置时间标签显示格式
t = arange(0.0, 120.0, 1)
s = sin(0.1*pi*t)*exp(-t*0.01)


dates = [now + datetime.timedelta(i) for i in range(120)]#
dates.sort()
datestr =[]


for i in range(0,12):
    datestr.append(dates[0]+i*days)
plt.xticks(datestr)#设置日期标签


plot(dates,s,'--r*')#画图,虚线--,红色r,数据点为星号*
xlabels = ax.get_xticklabels()
for xl in xlabels:
    xl.set_rotation(15) #把x轴上的label旋转15度,以免太密集时有重叠


show()

运行结果:


阅读更多
换一批

没有更多推荐了,返回首页