Python 使用NaN值在matplotlib中打印日期

Python 使用NaN值在matplotlib中打印日期,python,datetime,matplotlib,Python,Datetime,Matplotlib,我在这个主题上发现了很多问题,但我仍然无法理解用python绘制日期信息的基本步骤 我的时间序列是 >>> 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,

我在这个主题上发现了很多问题,但我仍然无法理解用python绘制日期信息的基本步骤

我的时间序列是

 >>> 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)
我的数据是

 >>> data.shape
(8736,)   
#contains np.nan values!!! 
我现在的代码(我的注释是我已经尝试过的…)

这将生成一个空白绘图


如果有人能告诉我绘制日期时间的步骤,我就是不知道从哪里开始,因为文档和stackoverflow答案似乎都有不同的方法。。(例如,使用plot_date而不是plt.plot()

我不知道为什么你的数据没有被绘制成图表-它甚至可以与NaN值一起工作。看看这个例子,你可以查看数据日期时间_系列,看看它们如何与你的数据进行比较。如评论中所述,有一个关于如何在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()

关于如何使用日期有一个详细的说明。
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()