2017-05-22 9 views
0

この件に関して多くの質問がありましたが、Pythonで日付情報をプロットするための基本的な手順を理解できません。matplotlibの日付をNaN値でプロットする

私の時系列は

>>> datetime_series.shape 
(8736,) 
>>> datetime_series 
array([datetime.datetime(1979, 1, 2, 0, 0), 
     datetime.datetime(1979, 1, 2, 1, 0), 
     datetime.datetime(1979, 1, 2, 2, 0), ..., 
     datetime.datetime(1979, 12, 31, 21, 0), 
     datetime.datetime(1979, 12, 31, 22, 0), 
     datetime.datetime(1979, 12, 31, 23, 0)], dtype=object) 

私のデータは今(私のコメントアウトは、私が試したものです...)

fig,ax1 = plt.subplots() 
plt.plot(datetime_series,data) 
#plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m')) 
#plt.gca().xaxis.set_major_locator(mdates.DayLocator()) 
#plt.gcf().autofmt_xdate() 
#ax1.set_xlim([datetime_series[0],datetime_series[-1]) 
ax1.set_ylabel('Cumulative windspeed over open water [m/s]') 
#ax1.set_xlim(### how do I do this?? 
plt.title('My title') 
fig.tight_layout() 
plt.show() 

これは生産

>>> data.shape 
(8736,) 
#contains np.nan values!!! 

私のコードです空白のプロット。

誰もがdatetimeをプロットする手順を教えてもらえれば、ドキュメントやstackoverflowの回答にはすべて異なる方法があるように見えるので、どこから開始するのか分かりません(たとえば、plot_dateとplt.plot()

+1

日付を使用する方法について[公式例](https://matplotlib.org/2.0.1/examples/api/date_demo.html)があります。 – ImportanceOfBeingErnest

答えて

1

あなたのデータはグラフ化されていない理由を私はよく分からない - 。それもNaN値で動作するはずです。この例をチェックアウト、あなたはどのように見てデータdatetime_seriesを見ることができますコメントに記載されているように、matplotlibで日付を使用する方法についての公式の例があります。これは見て分かる価値があります。

import matplotlib.pyplot as plt 
import datetime 
import numpy as np 

# Generate some dummy data 
N = 500 
year = np.random.randint(1950,2000,N) 
month = np.random.randint(1,12,N) 
day = np.random.randint(1,28,N) 

datatime_series = np.array([datetime.datetime(*(dd+(0,0))) for dd in zip(year, month, day)]) 
datatime_series.sort() 

data = np.random.random(N)*20000 + (np.linspace(1950,2000,N)/10.)**2 

# Now add some NaNs 
for i in np.random.randint(0,N-1,int(N/10)): 
    data[i]=np.nan 

fig, ax = plt.subplots(1) 
ax.plot(datatime_series, data) 
ax.set_ylim(0,1.2*ax.get_ylim()[1]) 
fig.autofmt_xdate() 
fig.show() 

enter image description here

関連する問題