Python Matplotlib pyplot - x轴值不适合数据

时间:2017-02-16 21:40:24

标签: python-3.x matplotlib

(对于Windows使用python 3.5 x64)

你好!

我在特定的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

1 个答案:

答案 0 :(得分:0)

你可以在绘图时减去偏移量,如下所示:

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

所有情节。