title: Saving Matplotlib Output to DBFS on Databricks
body: <p>I'm writing Python code on Databricks to process some data and output graphs. I want to be able to save these graphs as a picture file (.png or something, the format doesn't really matter) to DBFS.</p>
  
  <p>Code: </p>
  
  <pre><code>import pandas as pd
  import matplotlib.pyplot as plt
  
  df = pd.DataFrame({'fruits':['apple','banana'], 'count': [1,2]})
  plt.close()
  df.set_index('fruits',inplace = True)
  df.plot.bar()
  # plt.show()
  </code></pre>
  
  <p>Things that I tried:</p>
  
  <pre><code>plt.savefig("/FileStore/my-file.png")
  </code></pre>
  
  <blockquote>
    <p>[Errno 2] No such file or directory: '/FileStore/my-file.png'</p>
  </blockquote>
  
  <pre><code>fig = plt.gcf()
  dbutils.fs.put("/dbfs/FileStore/my-file.png", fig)
  </code></pre>
  
  <blockquote>
    <p>TypeError:  has the wrong type - (,) is expected.</p>
  </blockquote>
  
  <p>After some research, I think the fs.put only works if you want to save text files.</p>
  
  <p>running the above code with <code>plt.show()</code> will get you a bar graph - I want to be able to save the bar graph as an image to DBFS. Any help is appreciated, thanks in advance!</p>