2017-07-10 21 views
0

我有15分鐘的時間間隔記錄數據。我想繪製這些數據,並讓x軸在每天顯示一個小刻度,並且每個月都會顯示一個重要的刻度。下面是我嘗試使用一月和二月繪製十五分鐘的間隔數據,以x軸爲單位記錄日期和月份

#!/usr/bin/env python3 
import numpy as np 
import datetime as dt 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 

#make some data 
t = np.arange(0, 5664, 1) # Jan and Feb worth of 15 minute steps 
s = np.sin(2*np.pi*t) # data measured 

#plot the data 
fig, ax = plt.subplots() 
ax.plot(t, s) 

#select formatting 
days = mdates.DayLocator() 
daysfmt = mdates.DateFormatter('%d') 
months = mdates.MonthLocator() 
monthsfmt = mdates.DateFormatter('\n%b') 

的文件和其他Q & A的我已經閱讀並試圖在我的腦海拼湊做一個片段,我明白,我需要告訴matplotlib我想用於x軸的格式。這是我感到困惑的地方。我無法弄清楚如何指示繪圖數據是每15分鐘(每小刻度1440個樣本),所以當我顯示繪圖時,圖上沒有顯示任何內容。或者至少,我認爲這是因爲...

#apply formatting 
ax.xaxis.set_major_locator(months) 
ax.xaxis.set_major_formatter(monthsfmt) 
ax.xaxis.set_minor_locator(days) 
ax.xaxis.set_minor_formatter(daysfmt) 

#select dates 
datemin = dt.datetime.strptime('01/01/17', '%d/%m/%y') 
datemax = dt.datetime.strptime('28/02/17', '%d/%m/%y') 
ax.set_xlim(datemin, datemax) 

plt.show() 

疊加結果

回答

0

在那裏,工作;)

有兩個問題與您的代碼:

  1. 您正在繪製t和s,但將x軸設置爲錯誤的刻度

  2. 你只繪製罪的數值噪聲(x)的,因爲你總是使用2PI作爲參數跌倒,和sin(N * 2PI)= 0的倍數,對於任何整數n

完整代碼:

#!/usr/bin/env python3 
import num 

py as np 
import datetime as dt 
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates 



#make some data 
t = np.arange(0, 5664, 0.1) # Jan and Feb worth of 15 minute steps 
s = np.sin(2*np.pi*t) # data measured 

#plot the data 
fig, ax = plt.subplots() 
#ax.plot(t, s) 

#select formatting 
days = mdates.DayLocator() 
daysfmt = mdates.DateFormatter('%d') 
months = mdates.MonthLocator() 
monthsfmt = mdates.DateFormatter('\n%b') 

#apply formatting 
ax.xaxis.set_major_locator(months) 
ax.xaxis.set_major_formatter(monthsfmt) 
ax.xaxis.set_minor_locator(days) 
ax.xaxis.set_minor_formatter(daysfmt) 

#select dates 
datemin = dt.datetime.strptime('01/01/17', '%d/%m/%y') 
datemax = dt.datetime.strptime('28/02/17', '%d/%m/%y') 


t0 = dt.datetime(2017,1,1) 
t_datetime = [ t0 + dt.timedelta(minutes=15*t_) for t_ in t ] 

ax.plot(t_datetime,s) 

ax.set_xlim(t0, t_datetime[-1]) 


plt.show() 
相關問題