2012-05-18 7 views
2

csv 파일에서 시간 대 pps 수를 나타내는 스크립트를 작성하고 있습니다. 모든 것은이 시점에서 작동하지만 틱/틱 레이블이 X 축에서 발생하는 간격을 변경하는 방법을 파악할 수없는 것 같습니다. 기본값 대신 60 타임 스탬프/틱이 필요합니다.Python - Matplotlib - X 축 범위 설정 - 초당 패킷 표시

import matplotlib 
matplotlib.use('Agg')     
from matplotlib.mlab import csv2rec  
import matplotlib.pyplot as plt 
from matplotlib.ticker import MaxNLocator 
from pylab import * 

data = csv2rec('tpm_counter.log', names=['packets', 'time']) # reads in the data from the csv as column 1 = tweets column 2 = time 
rcParams['figure.figsize'] = 12, 4       # this sets the ddimensions of the graph to be made 
rcParams['font.size'] = 8 

fig = plt.figure() 
plt.plot(data['time'], data['packets'])      # this sets the fields to be graphed 
plt.xlabel("Time(minutes)")         # this sets the x label 
plt.ylabel("Packets")          # this sets the y label 
plt.title("Packets Capture Log: Packets Per Minute")   # this sets the title 

#plt.xticks(range(60)) --- nothing shows on the graph if I use this 

fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left') 

plt.savefig('tpm.png')          # this sets the output file name 

내가 plt.xticks(range(60))을 시도했지만 줄거리는 생성 할 때, 그것은 아무것도가 없습니다 : 내가 어디에 있어요 여기.

답변

2

date demo을 살펴보십시오.

HourLocator 또는 MinuteLocator와 함께 DateFormatter을 사용할 수 있습니다.

import matplotlib.dates as mdates 
import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
ax.plot_date(data['time'], data['packets']) 
hours = mdates.HourLocator() 
fmt = mdates.DateFormatter('%H:%M') 
ax.xaxis.set_major_locator(hours) 
ax.xaxis.set_major_formatter(fmt) 
+0

부끄러움 .. 감사합니다! – secumind

3

위의 대답은 작동합니다. 그러나 플롯에서 xticks와 xlabels를 재조정하는 좀 더 일반적인 방법을 다른 사람들에게 알리는 것이 도움이 될 수 있습니다. csv 파일을 사용하는 대신 예제 데이터를 생성했습니다.

import matplotlib    
import matplotlib.pyplot as plt 
from pylab import * 

time=range(5000) #just as an example 
data=range(5000) # just as an example 

fig = plt.figure() 
plt.plot(time,data)      # this sets the fields to be graphed 
plt.xlabel("Every 60th point")   # this sets the x label 
plt.ylabel("Data")      # this sets the y label 
plt.title("Rescaling axes")    # this sets the title 

#Slice the data into every 60th point. We want ticks at these points 
tickpos=data[::60] 
#Now create a list of labels for each point... 
ticklabels=[] 
for point in tickpos: 
    ticklabels.append(str(point/60)) 

plt.xticks(tickpos,ticklabels) # set the xtick positions and labels 

plt.savefig('tpm.png')