Show More
@@ -1,83 +1,81 b'' | |||
|
1 | 1 | """Produce SVG versions of active plots for display by the rich Qt frontend. |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | from __future__ import print_function |
|
7 | 7 | |
|
8 | 8 | # Standard library imports |
|
9 | 9 | import sys |
|
10 | 10 | |
|
11 | 11 | # Third-party imports |
|
12 | 12 | import matplotlib |
|
13 | 13 | from matplotlib.backends.backend_svg import new_figure_manager |
|
14 | 14 | from matplotlib._pylab_helpers import Gcf |
|
15 | 15 | |
|
16 | 16 | # Local imports. |
|
17 | 17 | from IPython.core.displaypub import publish_display_data |
|
18 | 18 | from IPython.lib.pylabtools import figure_to_svg |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | # Functions |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | |
|
24 |
def show(close= |
|
|
24 | def show(close=True): | |
|
25 | 25 | """Show all figures as SVG payloads sent to the IPython clients. |
|
26 | 26 | |
|
27 | 27 | Parameters |
|
28 | 28 | ---------- |
|
29 | 29 | close : bool, optional |
|
30 | 30 | If true, a ``plt.close('all')`` call is automatically issued after |
|
31 | 31 | sending all the SVG figures. If this is set, the figures will entirely |
|
32 | 32 | removed from the internal list of figures. |
|
33 | 33 | """ |
|
34 | 34 | for figure_manager in Gcf.get_all_fig_managers(): |
|
35 | 35 | send_svg_figure(figure_manager.canvas.figure) |
|
36 | 36 | if close: |
|
37 | 37 | matplotlib.pyplot.close('all') |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | # This flag will be reset by draw_if_interactive when called |
|
41 | 41 | show._draw_called = False |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | def draw_if_interactive(): |
|
45 | 45 | """ |
|
46 | 46 | Is called after every pylab drawing command |
|
47 | 47 | """ |
|
48 | 48 | # We simply flag we were called and otherwise do nothing. At the end of |
|
49 | 49 | # the code execution, a separate call to show_close() will act upon this. |
|
50 | 50 | show._draw_called = True |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def flush_svg(): |
|
54 | 54 | """Call show, close all open figures, sending all SVG images. |
|
55 | 55 | |
|
56 | 56 | This is meant to be called automatically and will call show() if, during |
|
57 | 57 | prior code execution, there had been any calls to draw_if_interactive. |
|
58 | 58 | """ |
|
59 | 59 | if show._draw_called: |
|
60 | # Show is called with the default close=False here, otherwise, the | |
|
61 | # Figure will be closed and not available for future plotting. | |
|
62 | 60 | show() |
|
63 | 61 | show._draw_called = False |
|
64 | 62 | |
|
65 | 63 | |
|
66 | 64 | def send_svg_figure(fig): |
|
67 | 65 | """Draw the current figure and send it as an SVG payload. |
|
68 | 66 | """ |
|
69 | 67 | # For an empty figure, don't even bother calling figure_to_svg, to avoid |
|
70 | 68 | # big blank spaces in the qt console |
|
71 | 69 | if not fig.axes: |
|
72 | 70 | return |
|
73 | 71 | |
|
74 | 72 | svg = figure_to_svg(fig) |
|
75 | 73 | # flush text streams before sending figures, helps a little with output |
|
76 | 74 | # synchronization in the console (though it's a bandaid, not a real sln) |
|
77 | 75 | sys.stdout.flush(); sys.stderr.flush() |
|
78 | 76 | publish_display_data( |
|
79 | 77 | 'IPython.zmq.pylab.backend_inline.send_svg_figure', |
|
80 | 78 | 'Matplotlib Plot', |
|
81 | 79 | {'image/svg+xml' : svg} |
|
82 | 80 | ) |
|
83 | 81 |
General Comments 0
You need to be logged in to leave comments.
Login now