Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update FAQ re: batch/webserver use. #13062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2019
Merged

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Dec 29, 2018

The previous FAQ entry re: not having a window appear is far too
complicated (in practice, just not calling show() is good enough,
especially now that we'll select a noninteractive backend when working
headlessly anyways.

The previous FAQ entry re: webserver use is also far too complicated;
again the choice of backend is now automatic, and we don't need to
select a priori what kind of output will be used (Matplotlib will switch
the underlying backend automatically).

Closes #12628.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

The previous FAQ entry re: not having a window appear is far too
complicated (in practice, just not calling show() is good enough,
especially now that we'll select a noninteractive backend when working
headlessly anyways.

The previous FAQ entry re: webserver use is also far too complicated;
again the choice of backend is now automatic, and we don't need to
select a priori what kind of output will be used (Matplotlib will switch
the underlying backend automatically).
@anntzer anntzer added this to the v3.1 milestone Dec 29, 2018
import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.savefig('myfig')
Simply do not call `~matplotlib.pyplot.show`, and directly save the figure to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work in an interactive terminal if we are in 'interactive' mode (as we call show() implicitly on figure creation).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough (that's not a use case I thought about). Do you want to recommend calling ioff(), or use('agg')? (I admit I never ever use ioff(), so heh.)


import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.savefig('myfig.png')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a new problem, but this should probably include a note about closing the figure or you will eventually run out of memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually perhaps the whole thing should just point to the same approach as the webserver solution (i.e. call the Figure() ctor) outside of pyplot. Thoughts?

In general, the simplest solution when using Matplotlib in a web server is
to completely avoid using pyplot (pyplot maintains references to the opened
figures to make `~.matplotlib.pyplot.show` work, but this will cause memory
leaks unless the figures are properly closed). Since Matplotlib 3.1, one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads like saving to the buffer is a feature of 3.1 (rather than the more terse version of getting a functional Figure).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to push a reword.

@jklymak
Copy link
Member

jklymak commented Feb 22, 2019

Merging as improvement...

@jklymak jklymak merged commit 49c9904 into matplotlib:master Feb 22, 2019
@anntzer anntzer deleted the webserver-doc branch February 22, 2019 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write canonical example of how to use Matplotlib inside a webserver
4 participants