title: Saving matplotlib subplot figure to image file
body: <p>I'm fairly new to <code>matplotlib</code> and am limping along. That said, I haven't found an obvious answer to this question.</p>
  
  <p>I have a scatter plot I wanted colored by groups, and it looked like <a href="https://stackoverflow.com/questions/21654635/scatter-plots-in-pandas-pyplot-how-to-plot-by-category">plotting via a loop</a> was the <a href="https://stackoverflow.com/questions/43967663/scatter-plot-with-legend-colored-by-group-without-multiple-calls-to-plt-scatter/43967880#43967880">way to roll</a>.</p>
  
  <p>Here is my reproducible example, based on the first link above:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import pandas as pd
  from pydataset import data
  
  df = data('mtcars').iloc[0:10]
  df['car'] = df.index
  
  fig, ax = plt.subplots(1)
  plt.figure(figsize=(12, 9))
  for ind in df.index:
      ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
  ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
  # plt.show()
  # plt.savefig('file.png')
  </code></pre>
  
  <p>Uncommenting <code>plt.show()</code> yields what I want:</p>
  
  <p><a href="https://i.stack.imgur.com/LYBPj.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LYBPj.png" alt="good plot"></a></p>
  
  <p>Searching around, it looked like <code>plt.savefig()</code> is the way to save a file; if I re-comment out <code>plt.show()</code> and run <code>plt.savefig()</code> instead, I get a blank white picture. <a href="https://stackoverflow.com/questions/9012487/matplotlib-pyplot-savefig-outputs-blank-image">This question</a>, suggests this is cause by calling <code>show()</code> before <code>savefig()</code>, but I have it entirely commented out. <a href="https://stackoverflow.com/questions/19555525/saving-plots-axessubplot-generated-from-python-pandas-with-matplotlibs-savefi">Another question</a> has a comment suggesting I can save the <code>ax</code> object directly, but that cuts off my legend:</p>
  
  <p><a href="https://i.stack.imgur.com/7bRkr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7bRkr.png" alt="chopped legend"></a></p>
  
  <p>The same question has an alternative that uses <code>fig.savefig()</code> instead. I get the same chopped legend.</p>
  
  <p>There's <a href="https://stackoverflow.com/questions/19555525/saving-plots-axessubplot-generated-from-python-pandas-with-matplotlibs-savefi">this question</a> which seems related, but I'm not plotting a <code>DataFrame</code> directly so I'm not sure how to apply the answer (where <code>dtf</code> is the <code>pd.DataFrame</code> they're plotting):</p>
  
  <pre><code>plot = dtf.plot()
  fig = plot.get_figure()
  fig.savefig("output.png")
  </code></pre>
  
  <p>Thanks for any suggestions.</p>
  
  <hr>
  
  <p><strong>Edit:</strong> to test the suggestion below to try <code>tight_layout()</code>, I ran this and still get a blank white image file:</p>
  
  <pre><code>fig, ax = plt.subplots(1)
  plt.figure(figsize=(12, 9))
  for ind in df.index:
      ax.scatter(df.loc[ind, 'wt'], df.loc[ind, 'mpg'], label=ind)
  ax.legend(bbox_to_anchor=(1.05, 1), loc=2)
  fig.tight_layout()
  plt.savefig('test.png')
  </code></pre>