|
| 1 | +Maximum levels on log-scaled contour plots are now respected |
| 2 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 3 | +Figures are now less strictly coupled to backends (and relatedly, to `.pyplot`). |
| 4 | + |
| 5 | +In particular, standalone figures (created with the `.Figure` constructor) can now be |
| 6 | +registered with the `.pyplot` module by calling ``plt.figure(fig)``. This allows to |
| 7 | +show them with ``plt.show()`` as you would do with any figure created with pyplot |
| 8 | +factory methods such as ``plt.figure()`` or ``plt.subplots()``. |
| 9 | + |
| 10 | +When closing a shown figure window, the related figure is reset to the standalone |
| 11 | +state, i.e. it's not visible to pyplot anymore, but if you still hold a reference |
| 12 | +to it, you can continue to work with it (e.g. do ``fig.savefig()``, or re-add it |
| 13 | +to pyplot with ``plt.figure(fig)`` and then show it again). |
| 14 | + |
| 15 | +The following is now possible - though the example is exaggerated to show what's |
| 16 | +possible. In practice, you'll stick with much simpler versions for better |
| 17 | +consistency :: |
| 18 | + |
| 19 | + import matplotlib.pyplot as plt |
| 20 | + from matplotlib.figure import Figure |
| 21 | + |
| 22 | + # Create a standalone figure |
| 23 | + fig = Figure() |
| 24 | + ax = fig.add_subplot() |
| 25 | + ax.plot([1, 2, 3], [4, 5, 6]) |
| 26 | + |
| 27 | + # Register it with pyplot |
| 28 | + plt.figure(fig) |
| 29 | + |
| 30 | + # Modify the figure through pyplot |
| 31 | + plt.xlabel("x label") |
| 32 | + |
| 33 | + # Show the figure |
| 34 | + plt.show() |
| 35 | + |
| 36 | + # Close the figure window through the GUI |
| 37 | + |
| 38 | + # Continue to work on the figure |
| 39 | + fig.savefig("my_figure.png") |
| 40 | + ax.set_ylabel("y label") |
| 41 | + |
| 42 | + # Re-register the figure and show it again |
| 43 | + plt.figure(fig) |
| 44 | + plt.show() |
| 45 | + |
| 46 | +Technical detail: Standalone figures use `.FigureCanvasBase` as canvas. This is |
| 47 | +replaced by a backend-dependent subclass when registering with pyplot, and is |
| 48 | +reset to `.FigureCanvasBase` when the figure is closed. `.Figure.savefig` uses |
| 49 | +the current canvas to save the figure (if possible). Since `.FigureCanvasBase` |
| 50 | +is Agg-based any Agg-based backend will create the same file output. There may |
| 51 | +be slight differences for non-Agg backends; e.g. if you use "GTK4Cairo" as |
| 52 | +interactive backend, ``fig.savefig("file.png")`` may create a slightly different |
| 53 | +image depending on whether the figure is registered with pyplot or not. |
0 commit comments