简体   繁体   English

在同一图上绘制数据框

[英]Plotting dataframes on same plot

I have two dataframes: 我有两个数据框:

a.head()

             AAPL             SPY       date
0  1000000.000000  1000000.000000 2010-01-04
1   921613.643818   969831.805642 2010-02-04
2   980649.393244  1000711.933790 2010-03-04
3   980649.393244  1000711.933790 2010-04-04
4  1232535.257461  1059090.504583 2010-05-04

and

b.head()

                 date           test
0 2010-01-26 22:17:44  990482.664854
1 2010-03-09 22:37:17  998565.699784
2 2010-03-12 02:11:23  989957.374785
3 2010-04-05 18:01:37  994315.860439
4 2010-04-06 11:06:50  987887.723816

After I set the index for a and b ( set_index('date') ), I can use the pandas plot() function to create a nice plot with the date as the x-axis and the various columns as y-values. 设置ab的索引( set_index('date') )后,可以使用pandas plot()函数创建一个漂亮的图,其日期为x轴,各列为y值。 What I want to do is plot two dataframes with different indices on the same figure . 我想做的是在同一张图上绘制两个具有不同索引的数据框 As you can see from a and b , the indices are different, and I want to plot them on the same figure. ab可以看到,索引是不同的,我想将它们绘制在同一图上。

I tried merge and concat to join the dataframes together, but the resulting plot is not what I'd like because those functions insert numpy.NaN in places where the date is not the same, which makes discontinuities in my plots. 我尝试了mergeconcat来将数据框连接在一起,但是结果图不是我想要的,因为这些函数在日期不相同的地方插入numpy.NaN ,这使我的图不连续。 I can use pd.fillna() but this is not what I'd like, since I'd rather it just connect the points together rather than drop down to 0. 我可以使用pd.fillna()但这不是我想要的,因为我希望它只是将各点连接在一起而不是降低到0。

Assuming you want the same time scale on the x-axis, then you will need timestamps as the index for for a and b before concatenating the columns. 假设您希望在x轴上使用相同的时间刻度,那么在串联这些列之前,将需要时间戳作为ab的索引。

You can then use interpolation to fill in the missing data, optionally with ffill() as an additional operation if you want to fill forward past the last observed data point. 然后,您可以使用插值法来填充丢失的数据,如果要向前填充过去观察到的数据点,可以选择使用ffill()作为附加操作。

df = pd.concat([a, b.set_index('date')], axis=1)
df.interpolate(method='time').plot()  # interpolate(method='time').ffill()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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