title: Matplotlib time series historical vs prediction plot shifting one month in the x-axis
body: <p>I'm trying to plot historical and predicted data into a single line in a single plot. The data ranges from <strong>'2020-01-01'</strong> <strong>'2021-12-31'</strong>. When the plot is produced, it plots the data from one month shifted to right in the x-axis. My code is given as follows.</p>
  <pre><code>import pandas as pd
  
  index = pd.date_range(start='2020-01-01', end='2021-12-31', freq='M')
  series = pd.DataFrame({'num':np.random.randint(0, 50, size=(len(index)))}, index=index)
  
  def plot_series(series):
      import matplotlib.pyplot as plt
      import matplotlib.dates as mdates
      plt.style.use('ggplot')
      
      fig = plt.figure(figsize=(18, 6))
      ax = plt.axes()
      plt.plot(series.index, series.num.values, color='black', label='Historical')
      plt.plot(series.index[18:], series.num.values[18:], color='red', label='Forecasted')
      ax.yaxis.set_ticks(np.arange(0, 105, 5))
      ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
      ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
      plt.legend(loc='best')
      plt.title(&quot;Time Series Plot: Historial to Forecasted&quot;)
      plt.xlabel(&quot;Month&quot;)
      plt.ylabel(&quot;Score&quot;);
      plt.gcf().autofmt_xdate(rotation=80)
      plt.show();
      return fig
  </code></pre>
  <p>Here is the resulting plot.
  <a href="https://i.stack.imgur.com/DzxN2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DzxN2.png" alt="enter image description here" /></a></p>
  <p>Why this behavior is happening and how can fix it?</p>