title: Matplotlib figure facecolor alpha while saving (background color, transparency)
body: <p>A previous question dealt with using <code>savefig()</code> to save with the same facecolor (background color) as is displayed on screen, namely:</p>
  
  <pre><code>fig = plt.figure()
  fig.patch.set_facecolor('blue')
  fig.savefig('foo.png', facecolor=fig.get_facecolor())
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/4804005/matplotlib-figure-facecolor-background-color">Matplotlib figure facecolor (background color)</a></p>
  
  <p>(Using <code>savefig()</code> requires us to respecify the background color.)</p>
  
  <p>I can also specify an alpha for transparency, e.g.: <a href="https://stackoverflow.com/questions/4581504/how-to-set-opacity-of-background-colour-of-graph-wit-matplotlib">How to set opacity of background colour of graph wit Matplotlib</a></p>
  
  <p><code>fig.patch.set_alpha(0.5)</code></p>
  
  <p>I can't find a way to make the figure with a transparent facecolor save as it appears onscreen.  The documentation seems incomplete on this: <a href="http://matplotlib.org/faq/howto_faq.html#save-transparent-figures" rel="noreferrer">http://matplotlib.org/faq/howto_faq.html#save-transparent-figures</a> - the actual saving isn't showing.  Using <code>transparent=True</code> with <code>savefig()</code> does not have the desired effect of making the facecolor transparent, instead it seems to make everything except axes legend transparent on top of that color (including graph backgrounds).</p>
  
  <p>EDIT: Some relevant extracts of code:</p>
  
  <pre><code>def set_face_color(fig, facecolor):
      if facecolor is False:
          # Not all graphs get color-coding
          facecolor = fig.get_facecolor()
          alpha = 1
      else:
          alpha = 0.5
      color_with_alpha = colorConverter.to_rgba(
          facecolor, alpha)
      fig.patch.set_facecolor(color_with_alpha)
  
  def save_and_show(plt, fig, save, disp_on, save_as):
      if save:
          plt.savefig(save_as, dpi=dpi_file, facecolor=fig.get_facecolor(),
                      edgecolor='none')
      if disp_on is True:
          figManager = plt.get_current_fig_manager()
          figManager.window.showMaximized()
          plt.show()
      else:
          plt.close('all')
  </code></pre>
  
  <p>It might be possible to combine these, but I often call <code>set_face_color()</code> at the beginning of a graphing function before I build up a grid of subplots and then <code>save_and_show()</code> toward the end.  I guess it should work in either place, but optimally I'd prefer to keep the functions separate and be able to extract the alpha to pass to <code>savefig()</code> from the final fig.</p>
  
  <p>EDIT 2 - Worth a thousand words</p>
  
  <p>Alpha = 0.5 on the left, 1 on the right.</p>
  
  <p><img src="https://i.stack.imgur.com/8LKwA.jpg" alt="image of code below"></p>
  
  <pre><code>t = [1, 2, 3, 4, 5]
  fig = plt.figure()
  fig.patch.set_alpha(0.5)
  fig.set_facecolor('b')
  plt.plot(t, t)
  fig2 = plt.figure()
  fig2.set_facecolor('b')
  plt.plot(t,t)
  </code></pre>