title: Create a boxplot with regression line using matplotlib
body: <p>I have a data frame with a column for "year" and a column for variable "FrostDays", with multiple values for each year.  I want to create a boxplot showing the distribution of values for each year, and add a regression line showing the change in the average number of FrostDays over a 50-year period.</p>
  
  <p>I can create the boxplot and the linear regression plot separately, but I cannot get Python to plot the two simultaneously on the same graph (on the same axis).  The result should have ~50 box plots with the y-axis showing FrostDays and the x-axis showing the year, and a regression line going through the boxplots according to the linear model.</p>
  
  <pre><code># Create the linear model
  
  x = PRISM_FD_A.year
  y = PRISM_FD_A.FrostDays
  stats = linregress(x, y)
  m = stats.slope
  b = stats.intercept
  xmin = min(PRISM_FD_A.year)
  xmax = max(PRISM_FD_A.year)
  ymin = min(PRISM_FD_A.FrostDays)
  ymax = max(PRISM_FD_A.FrostDays)
  prd = max(PRISM_FD_A.year) - min(PRISM_FD_A.year)
  ch = m * prd
  ch_FD = ch.astype(int)
  string = ("Total Change: %s days over %s years") % (ch_FD, prd)
  r = stats.rvalue
  r2 = round(((r)**2), 3)
  rstring = "R-squared: %s" % r2
  
  # Create the boxplot
  
  ax = PRISM_FD_A.boxplot(by='year',
                         column='FrostDays',
                         grid=False)
  ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
  plt.show()
  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/7ngcW.png" rel="nofollow noreferrer">Boxplot</a></p>
  
  <pre><code># Create the regression line:
  
  fig = plt.figure()
  fig.suptitle('Annual Count of Frost Days \n Ashokan Basin', fontsize=14, fontweight='bold')
  ax = fig.add_subplot(111)
  fig.subplots_adjust(top=0.85, bottom=0.15)
  ax.set_title(string, fontsize=10)
  ax.set_xlabel("year \n Source:  PRISM", fontsize=10)
  ax.set_ylabel("Number of Frost Days", fontsize=10)
  ax.plot(a_x, a_m * a_x + a_b, color="red", linewidth=3)
  fig.text(0.80, 0.015, rstring, color='white', backgroundcolor='royalblue',
           weight='roman', size='medium')
  ax.axis([xmin, xmax, ymin, ymax])
  plt.show()
  
  # Join the two together:
  
  fig = plt.figure()
  fig.suptitle('Annual Count of Frost Days \n Ashokan Basin', fontsize=14, fontweight='bold')
  ax = fig.add_subplot(111)
  fig.subplots_adjust(top=0.85, bottom=0.15)
  ax.set_xlabel("Year", fontsize=10)
  ax.set_ylabel("Number of Frost Days", fontsize=10)
  PRISM_FD_A.boxplot(by='year',
                         column='FrostDays',
                         grid=False)
  ax.xaxis.set_major_locator(ticker.MultipleLocator(5))
  ax.plot(a_x, a_m * a_x + a_b, color="red", linewidth=3)
  fig.text(0.80, 0.015, a_rstring, color='white', backgroundcolor='royalblue',
           weight='roman', size='medium')
  ax.axis([xmin, xmax, ymin, ymax])
  plt.show()
  </code></pre>
  
  <p>I get a plot with just the regression line and no box plot.</p>
  
  <p><a href="https://i.stack.imgur.com/DDQGf.png" rel="nofollow noreferrer">Regression line</a></p>