matplotlib和datetime的问题

2024-09-27 09:37:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用matplotlib绘制时间序列参数的观察值和计算值。观察到的数据存储在一个XL文件中,我使用openpyxl读取该文件并将其转换为pandas dataframe。模拟值被读取为经过的天数,我使用

delt = get_simulated_time()               
t0 = np.datetime64('2004-01-01T00:00:00')
tsim = t0 + np.asarray(delt).astype('timedelta64[D]')

我使用以下代码片段绘制数据

df = obs_data_df.query("block=='blk-7'")
pobs = df['pres']
tobs = df['date'] 
tobs = np.array(tobs, dtype='datetime64')
print(type(tobs), np.min(tobs), np.max(tobs))
axs.plot(tobs, pobs, '.', color='g', label='blk-7, obs', markersize=8)

tsim = np.array(curr_sim_obj.tsim, dtype='datetime64')
print(type(tsim), np.min(tsim), np.max(tsim))
axs.plot(tsim, curr_sim_obj.psim[:, 0], '-', color='g', label='blk-7, sim', linewidth=1)

打印报表的结果如下:

print(type(tobs), np.min(tobs), np.max(tobs))
... <class 'numpy.ndarray'> 2004-06-01T00:00:00.000000000 2020-06-01T00:00:00.000000000
print(type(tsim), np.min(tsim), np.max(tsim))
... <class 'numpy.ndarray'> 2004-01-01T00:00:00 2020-07-20T00:00:00

这些类型看起来正常,但我从matplotlib收到以下错误消息: ValueError: view limit minimum -36907.706903627106 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units 我不明白为什么我会收到这个消息,因为打印语句表明数据是一致的。我试着进一步调查

print(np.dtype(tsim), np.min(tobs), np.max(tobs))

但是,请获取以下错误消息:

TypeError: data type not understood

这让我更加困惑,因为我在前面的语句中设置了tobs数据类型。我不得不说,我真的很困惑python、pandas和numpy处理日期的方式有什么不同,上面的各种代码混乱反映了我在处理过程中采用的变通方法。我想知道如何在同一个图上绘制两个不同的时间序列,所以所有的建议都非常受欢迎。提前谢谢你

更新: 在减少代码以获得再现错误的更简单案例的同时,我发现以下代码隐藏在绘图例程中: axs.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o') 这是测试绘图例程时留下的。一旦我删除了这个,错误就消失了。我想我需要更仔细地检查我的代码


Tags: 数据代码dftype错误np绘制min

热门问题