2017-02-16 77 views
0

(使用python 3.5 x64 for windows)Python Matplotlib pyplot - 不符合数据的x轴值

嗨!

在特定的unix时间使用整数格式的数据。 我有一个问题,我希望x轴(unix时间)是“秒以来的第一次记录以来的时间”,但是这个轴的值不适合这个。我的意思是:y轴的第一个整数没有设置在x轴的0值处。 如何更改x轴的值以适应我的需求? 我尝试了几件事:xticks,axes.set_ylim()...但总是遇到一个我无法解决的问题。 xticks可以工作,但我不知道如何适应UNIX时间在使CPM和时间之间的相关性不迷失方向......

def plot_overview2(selector = None): 
global logtime, logtime_delta, cpm, color 

plt.figure(figsize=(14,7), dpi=70, facecolor="none")  #was figsize=(20,10),dpi=70,facecolor="none" - filled whole screen 
plt.suptitle('CPM ', fontsize=16, fontweight='bold') 
plt.subplots_adjust(hspace=None, wspace=.2, left=.05, top=.95, bottom=.07, right=.98) 
plt.subplot(1, 1, 1) 
plt.grid(True) 

# add a label to the x and y axis 
plt.xlabel('Time since first record [sec]')  
plt.ylabel("CPM") 

# define the x-axis limits 
xmin = logtime.min() # e.g. 1483049960.0 
xmax = logtime.max() # e.g. 1483295877.0 
plt.xlim(xmin, xmax) # if commented out then the plot will find its own limits 
#plt.xticks(1, logtime_delta) 

# define the y-axis limits 
#plt.ylim(0, 150) # if commented out then the plot will find its own limits 

recmax = cpm.size  # allows to limit the data range plotted 
# plot the raw data 
plt.plot(logtime[:recmax],cpm[:recmax], color=color['cpm'], linewidth=.75, label ="") #linewidth was .5, logtime[:recmax],cpm[:recmax] 
#plt.plot_date(logtime, cpm, color=color['cpm'], linewidth=.75, label ="") 

# plot the moving average over N datapoints with red on yellow line background 
# skip the first and last N/2 data points, which are meaningless due to averaging 
if len(logtime) < 300: 
    N=len(logtime)+1/2 
else: 
    N=300 
plt.plot(logtime[N//2:recmax - N//2], np.convolve(cpm, np.ones((N,))/N, mode='same')[N//2:recmax - N//2], color="yellow", linewidth=6, label ="") 
plt.plot(logtime[N//2:recmax - N//2], np.convolve(cpm, np.ones((N,))/N, mode='same')[N//2:recmax - N//2], color="red", linewidth=2, label ="MovAvg, N="+str(N)) 

# plot the line for the average 
av  = np.empty(recmax) 
npav = np.average(cpm[:recmax]) 
av[:] = npav 
plt.plot(logtime[:recmax], av[:recmax], color=color['MW'], linewidth=2, label= "Average CPM={0:6.3f}".format(npav)) 

# plot the legend in the upper left corner 
plt.legend(bbox_to_anchor=(1.01, .9), loc=2, borderaxespad=0.) 
plt.legend(loc='upper left') 

我真的新的Python。所以你能否给出简单的答案。 :) 谢谢!

第一绘图线开始于几乎20000

回答

0

也许你可以减去偏移,当你的情节,这样的事情:

plot(logtime[N//2:recmax - N//2] - logtime[0], ... 

所有地块。

+0

好吧,工作,并非常容易... xD谢谢! – Distelzombie

+0

没问题:)你能接受答案吗? –