简体   繁体   中英

Pandas datetime index not plotting properly

I have two dataframes with a datetime index. Each dataframe has data from the same day. I want to plot them on the same axis, but when I do, the data seem to come from different times. Here is a minimal working example.

The first frame. created_at should be the index.

created_at              pred
2018-01-21 06:00:00 7.548181517907031
2018-01-21 06:15:00 9.32126005682907
2018-01-21 06:30:00 12.600515378912815
2018-01-21 06:45:00 16.578908485745487
2018-01-21 07:00:00 20.80107311107899

The Second frame. created_at should be the index.

created_at           WR
2018-01-21 18:01:02 81.0
2018-01-21 17:34:51 77.0
2018-01-21 16:59:03 79.0
2018-01-21 16:29:36 81.0
2018-01-21 16:02:49 79.0

When I plot them using

ax = df1.plot()
df2.plot(ax = ax)

I get

在此处输入图片说明

Seems like the second dataframe's index is being interpreted incorrectly. Any idea why this might be?

This will create what you need

#df1.created_at=pd.to_datetime(df1.created_at)
#df2.created_at=pd.to_datetime(df2.created_at)
#df1=df1.set_index('created_at')
#df2=df2.set_index('created_at')

import matplotlib.pyplot as plt

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)

ax1.plot(df1['pred'])
ax1.plot(df2['WR'])

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM