Show More
@@ -1,116 +1,139 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_agg import new_figure_manager |
|
13 | from matplotlib.backends.backend_agg 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.config.configurable import SingletonConfigurable |
|
17 | from IPython.config.configurable import SingletonConfigurable | |
18 | from IPython.core.displaypub import publish_display_data |
|
18 | from IPython.core.displaypub import publish_display_data | |
19 | from IPython.lib.pylabtools import print_figure, select_figure_format |
|
19 | from IPython.lib.pylabtools import print_figure, select_figure_format | |
20 | from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum |
|
20 | from IPython.utils.traitlets import Dict, Instance, CaselessStrEnum | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | # Configurable for inline backend options |
|
22 | # Configurable for inline backend options | |
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | class InlineBackendConfig(SingletonConfigurable): |
|
25 | class InlineBackendConfig(SingletonConfigurable): | |
26 | """An object to store configuration of the inline backend.""" |
|
26 | """An object to store configuration of the inline backend.""" | |
27 |
|
27 | |||
28 | # The typical default figure size is too large for inline use, |
|
28 | # The typical default figure size is too large for inline use, | |
29 | # so we shrink the figure size to 6x4, and tweak fonts to |
|
29 | # so we shrink the figure size to 6x4, and tweak fonts to | |
30 | # make that fit. This is configurable via Global.pylab_inline_rc, |
|
30 | # make that fit. This is configurable via Global.pylab_inline_rc, | |
31 | # or rather it will be once the zmq kernel is hooked up to |
|
31 | # or rather it will be once the zmq kernel is hooked up to | |
32 | # the config system. |
|
32 | # the config system. | |
33 | rc = Dict({'figure.figsize': (6.0,4.0), |
|
33 | rc = Dict({'figure.figsize': (6.0,4.0), | |
34 | # 12pt labels get cutoff on 6x4 logplots, so use 10pt. |
|
34 | # 12pt labels get cutoff on 6x4 logplots, so use 10pt. | |
35 | 'font.size': 10, |
|
35 | 'font.size': 10, | |
36 | # 10pt still needs a little more room on the xlabel: |
|
36 | # 10pt still needs a little more room on the xlabel: | |
37 | 'figure.subplot.bottom' : .125 |
|
37 | 'figure.subplot.bottom' : .125 | |
38 | }, config=True, |
|
38 | }, config=True, | |
39 | help="""Subset of matplotlib rcParams that should be different for the |
|
39 | help="""Subset of matplotlib rcParams that should be different for the | |
40 | inline backend.""" |
|
40 | inline backend.""" | |
41 | ) |
|
41 | ) | |
42 | figure_format = CaselessStrEnum(['svg', 'png'], default_value='png', config=True, |
|
42 | figure_format = CaselessStrEnum(['svg', 'png'], default_value='png', config=True, | |
43 | help="The image format for figures with the inline backend.") |
|
43 | help="The image format for figures with the inline backend.") | |
44 |
|
44 | |||
45 | def _figure_format_changed(self, name, old, new): |
|
45 | def _figure_format_changed(self, name, old, new): | |
46 | if self.shell is None: |
|
46 | if self.shell is None: | |
47 | return |
|
47 | return | |
48 | else: |
|
48 | else: | |
49 | select_figure_format(self.shell, new) |
|
49 | select_figure_format(self.shell, new) | |
50 |
|
50 | |||
51 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
51 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | #----------------------------------------------------------------------------- |
|
54 | #----------------------------------------------------------------------------- | |
55 | # Functions |
|
55 | # Functions | |
56 | #----------------------------------------------------------------------------- |
|
56 | #----------------------------------------------------------------------------- | |
57 |
|
57 | |||
58 |
def show(close= |
|
58 | def show(close=False): | |
59 | """Show all figures as SVG payloads sent to the IPython clients. |
|
59 | """Show all figures as SVG payloads sent to the IPython clients. | |
60 |
|
60 | |||
61 | Parameters |
|
61 | Parameters | |
62 | ---------- |
|
62 | ---------- | |
63 | close : bool, optional |
|
63 | close : bool, optional | |
64 | If true, a ``plt.close('all')`` call is automatically issued after |
|
64 | If true, a ``plt.close('all')`` call is automatically issued after | |
65 |
sending all the |
|
65 | sending all the figures. If this is set, the figures will entirely | |
66 | removed from the internal list of figures. |
|
66 | removed from the internal list of figures. | |
67 | """ |
|
67 | """ | |
68 | for figure_manager in Gcf.get_all_fig_managers(): |
|
68 | for figure_manager in Gcf.get_all_fig_managers(): | |
69 | send_figure(figure_manager.canvas.figure) |
|
69 | send_figure(figure_manager.canvas.figure) | |
70 | if close: |
|
70 | if close: | |
71 | matplotlib.pyplot.close('all') |
|
71 | matplotlib.pyplot.close('all') | |
|
72 | show._to_draw = [] | |||
|
73 | ||||
72 |
|
74 | |||
73 |
|
75 | |||
74 | # This flag will be reset by draw_if_interactive when called |
|
76 | # This flag will be reset by draw_if_interactive when called | |
75 | show._draw_called = False |
|
77 | show._draw_called = False | |
|
78 | # list of figures to draw when flush_figures is called | |||
|
79 | show._to_draw = [] | |||
76 |
|
80 | |||
77 |
|
81 | |||
78 | def draw_if_interactive(): |
|
82 | def draw_if_interactive(): | |
79 | """ |
|
83 | """ | |
80 | Is called after every pylab drawing command |
|
84 | Is called after every pylab drawing command | |
81 | """ |
|
85 | """ | |
82 | # We simply flag we were called and otherwise do nothing. At the end of |
|
86 | # signal that the current active figure should be sent at the end of execution. | |
83 | # the code execution, a separate call to show_close() will act upon this. |
|
87 | # Also sets the _draw_called flag, signaling that there will be something to send. | |
|
88 | # At the end of the code execution, a separate call to flush_figures() | |||
|
89 | # will act upon these values | |||
|
90 | ||||
|
91 | fig = Gcf.get_active().canvas.figure | |||
|
92 | ||||
|
93 | # ensure current figure will be drawn, and each subsequent call | |||
|
94 | # of draw_if_interactive() moves the active figure to ensure it is | |||
|
95 | # drawn last | |||
|
96 | try: | |||
|
97 | show._to_draw.remove(fig) | |||
|
98 | except ValueError: | |||
|
99 | # ensure it only appears in the draw list once | |||
|
100 | pass | |||
|
101 | show._to_draw.append(fig) | |||
84 | show._draw_called = True |
|
102 | show._draw_called = True | |
85 |
|
103 | |||
86 |
|
||||
87 | def flush_figures(): |
|
104 | def flush_figures(): | |
88 | """Call show, close all open figures, sending all figure images. |
|
105 | """Send all figures that changed | |
89 |
|
106 | |||
90 | This is meant to be called automatically and will call show() if, during |
|
107 | This is meant to be called automatically and will call show() if, during | |
91 | prior code execution, there had been any calls to draw_if_interactive. |
|
108 | prior code execution, there had been any calls to draw_if_interactive. | |
92 | """ |
|
109 | """ | |
93 | if show._draw_called: |
|
110 | if not show._draw_called: | |
94 | show() |
|
111 | return | |
95 | show._draw_called = False |
|
112 | # exclude any figures that were closed: | |
|
113 | active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()]) | |||
|
114 | for fig in [ fig for fig in show._to_draw if fig in active ]: | |||
|
115 | send_figure(fig) | |||
|
116 | # clear flags for next round | |||
|
117 | show._to_draw = [] | |||
|
118 | show._draw_called = False | |||
96 |
|
119 | |||
97 |
|
120 | |||
98 | def send_figure(fig): |
|
121 | def send_figure(fig): | |
99 | """Draw the current figure and send it as a PNG payload. |
|
122 | """Draw the current figure and send it as a PNG payload. | |
100 | """ |
|
123 | """ | |
101 | # For an empty figure, don't even bother calling figure_to_svg, to avoid |
|
124 | # For an empty figure, don't even bother calling figure_to_svg, to avoid | |
102 | # big blank spaces in the qt console |
|
125 | # big blank spaces in the qt console | |
103 | if not fig.axes: |
|
126 | if not fig.axes: | |
104 | return |
|
127 | return | |
105 | fmt = InlineBackendConfig.instance().figure_format |
|
128 | fmt = InlineBackendConfig.instance().figure_format | |
106 | data = print_figure(fig, fmt) |
|
129 | data = print_figure(fig, fmt) | |
107 | mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' } |
|
130 | mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' } | |
108 | mime = mimetypes[fmt] |
|
131 | mime = mimetypes[fmt] | |
109 | # flush text streams before sending figures, helps a little with output |
|
132 | # flush text streams before sending figures, helps a little with output | |
110 | # synchronization in the console (though it's a bandaid, not a real sln) |
|
133 | # synchronization in the console (though it's a bandaid, not a real sln) | |
111 | sys.stdout.flush(); sys.stderr.flush() |
|
134 | sys.stdout.flush(); sys.stderr.flush() | |
112 | publish_display_data( |
|
135 | publish_display_data( | |
113 | 'IPython.zmq.pylab.backend_inline.send_figure', |
|
136 | 'IPython.zmq.pylab.backend_inline.send_figure', | |
114 | {mime : data} |
|
137 | {mime : data} | |
115 | ) |
|
138 | ) | |
116 |
|
139 |
General Comments 0
You need to be logged in to leave comments.
Login now