简体   繁体   中英

Use one graph for multiple timeseries dataframes pandas

I am currently creating a pandas project on stock prices using time series. I have a column stock return that has negative and positive values on the y axis. I have data for the last 5 years and am trying to compare the months of each year in the same graph but when I plot them they separate along the dates rather than overlapping (image below) :

fig, ax = plt.subplots()
Monthly_Return2017.plot(ax=ax)
Monthly_Return2016.plot(ax=ax)
Monthly_Return2015.plot(ax=ax)
Monthly_Return2014.plot(ax=ax)
Monthly_Return2013.plot(ax=ax).axhline(y = 1, color = "black", lw = 2)
plt.show()

在此处输入图片helloption

So instead I want to have them split by months Jan-December on the x-axis. Is there anyway I can do this keep the data as a time series. Heres a sample of three of the dataframes.

    Close
Date    
2017-01-31  1.000000
2017-02-28  1.033158
2017-03-31  1.041128
2017-04-30  1.137012
2017-05-31  1.210934
2017-06-30  1.140489
2017-07-31  1.167811
2017-08-31  1.178893
2017-09-30  1.203717
2017-10-31  1.275920
2017-11-30  1.281906
2017-12-31  1.313270

Close
Date    
2016-01-31  1.000000
2016-02-29  0.939188
2016-03-31  1.002692
2016-04-30  0.932781
2016-05-31  0.990268
2016-06-30  0.931557
2016-07-31  1.034780
2016-08-31  1.032438
2016-09-30  1.046221
2016-10-31  1.055979
2016-11-30  1.020311
2016-12-31  1.038859

    Close
Date    
2015-01-31  1.000000
2015-02-28  1.044676
2015-03-31  1.025219
2015-04-30  1.010808
2015-05-31  1.000970
2015-06-30  0.979148
2015-07-31  1.176855
2015-08-31  1.163010
2015-09-30  1.144519
2015-10-31  1.337128
2015-11-30  1.396929
2015-12-31  1.427554

Any help would be greatly appreciated.

I would just create two different dataframes and forget about the dates for the plot and the time series. something like:

import pandas as pd
import matplotlib.pyplot as plt

Monthly_Return2017 = Monthly_Return2017.reset_index()
Monthly_Return2016 = Monthly_Return2016.reset_index()

fig, ax = plt.subplots()

Monthly_Return2017['Close'].plot(ax=ax)
Monthly_Return2016['Close'].plot(ax=ax)

Then you can play with xticks to put up the months. Does it help?

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