1

In Jupyter, if the last statement in a cell returns a matplotlib.Figure, the figure is displayed.

One can perform a similar trick with custom classes by providing a _repr_png_ function in that class. However, matplotlib Figures do not provide this function. This makes me wonder. How does Jupyter "know" what to do with a matplotlib figure?

Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
  • As Stef's excellent answer hints at, the plot generation no longer needs to be in the last line in modern, updated Jupyter, see [here](https://stackoverflow.com/a/73633497/8508004). In other words, you don't need the `plt.show()` necessarily anymore. – Wayne Nov 07 '22 at 20:50

1 Answers1

2

How does Jupyter "know" what to do with a matplotlib figure?

In the inline backend, this is achieved by registering a function to show changed figures (flush_figures) with the post_execute event in IPython, see configure_inline_support.

Stef
  • 28,728
  • 2
  • 24
  • 52