title: Error in Django when using matplotlib examples
body: <p>I am testing several cases of Django and matplotlib such as <a href="https://stackoverflow.com/questions/45460145/how-to-render-a-matplotlib-plot-in-a-django-web-application">this question</a> or <a href="https://www.science-emergence.com/Articles/Utiliser-Matplotlib-avec-Django/" rel="noreferrer">in french</a>.</p>
  
  <p>Each time, it works on my mac, but does not on my server, where I receive the following error:</p>
  
  <pre><code>Internal Server Error: /mj/charts/mplimage.png
  Traceback (most recent call last):
    File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 35, in inner
      response = get_response(request)
    File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 128, in _get_response
      response = self.process_exception_by_middleware(e, request)
    File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 126, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/root/src/jm/majority_judgment/views.py", line 39, in mplimage
      canvas.print_png(response)
    File "/usr/local/lib/python3.6/dist-packages/matplotlib/backends/backend_agg.py", line 526, in print_png
      with cbook.open_file_cm(filename_or_obj, "wb") as fh:
    File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__
      return next(self.gen)
    File "/usr/local/lib/python3.6/dist-packages/matplotlib/cbook/__init__.py", line 624, in open_file_cm
      fh, opened = to_filehandle(path_or_file, mode, True, encoding)
    File "/usr/local/lib/python3.6/dist-packages/matplotlib/cbook/__init__.py", line 615, in to_filehandle
      raise ValueError('fname must be a PathLike or file handle')
  ValueError: fname must be a PathLike or file handle
  [28/Mar/2018 19:09:11] "GET /mj/charts/mplimage.png HTTP/1.1" 500 82804
  </code></pre>
  
  <p>Here is a minimal snippet:</p>
  
  <pre><code>def mplimage(request):
      f = matplotlib.figure.Figure()
      canvas = FigureCanvasAgg(f)
      response = HttpResponse(content_type='image/png')
      canvas.print_png(response)
      plt.close(f)
      return response
  </code></pre>
  
  <p>I have tried to update matplotlib, django and so on, but it did nothing...</p>