Reputation: 11
I am trying to plot 2 time series in matplotlib. However, when I use plt.subplots(nrows=2, ncols=1) to plot both charts, the date index is showing 50 years in the past (so 1972 instead of 2022) even though the day and month are correct.
On the other hand, if I plot each time series separately without plt.subpots(nrows=2, ncols=1), I would get the year correct.
I attached a snapshot whereby if you check the x-axis, 1970 should be 2020, 1971 should be 2021, and so on.
Here's my code:
# PLotting
if plot:
legend_loc = 'upper right'
figsize = (20,12)
title_pad = 20
fig, ax = plt.subplots(figsize=figsize, nrows=2, ncols=1, sharex=True)
ax[0].plot(weighted_track_record, label=weighted_track_record.columns)
ax[0].legend(loc=legend_loc)
ax[0].set_title('Cryptocurrency Weight (No Rebalancing)', fontweight='bold', color=good_color, pad=title_pad)
ax[1].plot(portfolio_vs_benchmark, label=portfolio_vs_benchmark.columns)
ax[1].legend(loc=legend_loc)
ax[1].set_title(f'Portfolio Value vs {benchmark_id.upper()}', fontweight='bold', color=good_color, pad=title_pad)
# Format x-axis
ax[1].xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax[1].xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.xticks(rotation=90)
Note that both data frames weighted_track_record and portfolio_vs_benchmark share the exact same date index.
Thanks!
Upvotes: 1
Views: 275
Reputation: 584
There seem to be some interaction between Pandas and Matplotlib's date handling. (e.g. https://github.com/pandas-dev/pandas/issues/34850) You might want to try to update both to the latest versions and see if that helps
Upvotes: 0