1

I am plotting the following pandas MultiIndex DataFrame:

print(log_returns_weekly.head())

           AAPL      MSFT      TSLA        FB     GOOGL
Date Date                                                  
2016 1    -0.079078  0.005278 -0.155689  0.093245  0.002512
     2    -0.001288 -0.072344  0.003811 -0.048291 -0.059711
     3     0.119746  0.082036  0.179948  0.064994  0.061744
     4    -0.150731 -0.102087  0.046722  0.030044 -0.074852
     5     0.069314  0.067842 -0.075598  0.010407  0.056264

with the first sub-index representing the year, and the second one the week from that specific year.

This is simply achieved via the pandas plot() method; however, as seen below, the x-axis will not be in a (year, week) format i.e. (2016, 1), (2016, 2) etc. Instead, it simply shows 'Date,Date' - does anyone therefore know how I can overcome this issue?

log_returns_weekly.plot(figsize(8,8))

enter image description here

Jayjay95
  • 199
  • 9
  • Pandas does not have a plot function, it is just a wrapper for matplotlib's `plt.plot`. You should tag your question appropriately. And please read [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/8881141) and provide a reproducible dataset. – Mr. T Dec 01 '18 at 22:36

1 Answers1

0

You need to convert your multiindex to single one and add a day, so it would be like this: 2016-01-01.

log1 = log_returns_weekly.set_index(log_returns_weekly.index.map(lambda x: pd.datetime(*x,1)))
log1.plot()
Michael
  • 5,095
  • 2
  • 13
  • 35