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