Skip to content

Write canonical example of how to use Matplotlib inside a webserver #12628

Closed

Description

@tacaswell

As shown by #11094 #12612 matplotlib is used inside of webservers to dynamically serve plots. The example should show:

  1. importance of picking a non-GUI backend (both by setting the backend and explicitly by not using pyplot). Given that these servers are multi-threaded, not using pyplot at all may be the best option to avoid any coupling between workers via pyplot and memory leaks due to pyplot holding references to figures forever. Should probably show use of new_figure_manager
  2. saving to a bytesIO buffer to avoid disk
  3. png, svg, and pdf
  4. be applicable to both django and flask (not sure off the top of my head what the differences are), bonus points for cherrypy, aiohttp, tornado, and pyramid

Activity

  1. added this to the v3.0.0-doc milestone on Oct 25, 2018
  2. anntzer commented on Oct 25, 2018

    @anntzer
    Contributor

    Rather than bothering with any explicit backend at all, I would strongly suggest merging #12450 and have the canonical approach be

    from matplotlib.figure import Figure
    fig = Figure()
    <do the plotting>
    fig.savefig(...)
    

    Even with 3.0 (i.e. without that PR) new_figure_manager is totally overkill, just manually attaching a canvas (FigureCanvasAgg(fig) or even just FigureCanvasBase(fig)) is enough.
    (... except if you want to use a third-party renderer e.g. mplcairo, but I doubt that's the point of the proposed example)

  3. mgeier commented on Nov 3, 2018

    @mgeier
    Contributor

    Possible duplicate: #4124.

  4. tacaswell commented on Nov 3, 2018

    @tacaswell
    MemberAuthor

    Indeed! I'm going to close the older one.

  5. anntzer commented on Nov 3, 2018

    @anntzer
    Contributor

    Do you want to make a decision about #12450, by the way? :) (as noted above)

  6. modified the milestones: v3.0.0-doc, v3.0.2-doc on Nov 27, 2018
  7. anntzer commented on Nov 27, 2018

    @anntzer
    Contributor

    @tacaswell I think something like

    import base64
    from io import BytesIO
    
    from flask import Flask
    from matplotlib.figure import Figure
    
    
    app = Flask(__name__)
    
    
    @app.route("/")
    def hello():
        # Generate the figure **without using pyplot**.
        fig = Figure()
        ax = fig.subplots()
        ax.plot([1, 2])
        # Save it to a temporary buffer.
        buf = BytesIO()
        fig.savefig(buf, format="png")
        # Embed the result in the html output.
        data = base64.b64encode(buf.getbuffer()).decode("ascii")
        return f"<img src='data:image/png;base64,{data}'/>"
    

    is what you're looking for?

  8. story645 commented on Nov 27, 2018

    @story645
    Member

    My version was (stolen from a cookbook)

    fig = matplotlib.figure.Figure()
    canvas = FigureCanvas(fig)
    //plot to figure code
    canvas.print_figure(response)

    where the outfile is the responsebuffer, like

    response = pyramid.response.Response(content_type='image/png')
  9. anntzer commented on Nov 27, 2018

    @anntzer
    Contributor
    • canvas = FigureCanvas(fig) is not necessary anymore since Attach a FigureCanvasBase by default to Figures. #12450 (that's the whole point of that PR).
    • I prefer suggesting the use of savefig (and yes you can also pass the Response buffer to it), as it's a regular method that you'd also use in other contents, rather than print_figure, which is a bit an implementation detail.
  10. tacaswell commented on Dec 1, 2018

    @tacaswell
    MemberAuthor

    Yes, something as simple as that is what I had in mind.

    Agree on preferring savefig over print_figure.

  11. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Dec 6, 2018
  12. modified the milestones: v3.0-doc, v3.1.0 on Feb 23, 2019
  13. pjmnoble commented on Oct 2, 2019

    @pjmnoble

    Hi problem goes away really nicely using the AGG backend
    I just import matlplotlib at the top of my code and follow this with matplotlib.use('AGG') before importing any other matplotlib components (matplotlib.pyplot in this case)

    No need for the matplotlib GUI backend in the web server.

    Running on a Mac(High Sierra) with a Django server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Documentation🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions