Show More
@@ -1,119 +1,119 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 | from cStringIO import StringIO |
|
9 | from cStringIO import StringIO | |
10 |
|
10 | |||
11 | # System library imports. |
|
11 | # System library 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 backend_payload import add_plot_payload |
|
17 | from backend_payload import add_plot_payload | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Functions |
|
20 | # Functions | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | def show(close=True): |
|
23 | def show(close=True): | |
24 | """Show all figures as SVG payloads sent to the IPython clients. |
|
24 | """Show all figures as SVG payloads sent to the IPython clients. | |
25 |
|
25 | |||
26 | Parameters |
|
26 | Parameters | |
27 | ---------- |
|
27 | ---------- | |
28 | close : bool, optional |
|
28 | close : bool, optional | |
29 | If true, a ``plt.close('all')`` call is automatically issued after |
|
29 | If true, a ``plt.close('all')`` call is automatically issued after | |
30 | sending all the SVG figures. |
|
30 | sending all the SVG figures. | |
31 | """ |
|
31 | """ | |
32 | for figure_manager in Gcf.get_all_fig_managers(): |
|
32 | for figure_manager in Gcf.get_all_fig_managers(): | |
33 | send_svg_canvas(figure_manager.canvas) |
|
33 | send_svg_canvas(figure_manager.canvas) | |
34 | if close: |
|
34 | if close: | |
35 | matplotlib.pyplot.close('all') |
|
35 | matplotlib.pyplot.close('all') | |
36 |
|
36 | |||
37 | # This flag will be reset by draw_if_interactive when called |
|
37 | # This flag will be reset by draw_if_interactive when called | |
38 | show._draw_called = False |
|
38 | show._draw_called = False | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | def figsize(sizex, sizey): |
|
41 | def figsize(sizex, sizey): | |
42 | """Set the default figure size to be [sizex, sizey]. |
|
42 | """Set the default figure size to be [sizex, sizey]. | |
43 |
|
43 | |||
44 | This is just an easy to remember, convenience wrapper that sets:: |
|
44 | This is just an easy to remember, convenience wrapper that sets:: | |
45 |
|
45 | |||
46 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] |
|
46 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] | |
47 | """ |
|
47 | """ | |
48 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] |
|
48 | matplotlib.rcParams['figure.figsize'] = [sizex, sizey] | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def pastefig(*figs): |
|
51 | def pastefig(*figs): | |
52 | """Paste one or more figures into the console workspace. |
|
52 | """Paste one or more figures into the console workspace. | |
53 |
|
53 | |||
54 | If no arguments are given, all available figures are pasted. If the |
|
54 | If no arguments are given, all available figures are pasted. If the | |
55 | argument list contains references to invalid figures, a warning is printed |
|
55 | argument list contains references to invalid figures, a warning is printed | |
56 | but the function continues pasting further figures. |
|
56 | but the function continues pasting further figures. | |
57 |
|
57 | |||
58 | Parameters |
|
58 | Parameters | |
59 | ---------- |
|
59 | ---------- | |
60 | figs : tuple |
|
60 | figs : tuple | |
61 | A tuple that can contain any mixture of integers and figure objects. |
|
61 | A tuple that can contain any mixture of integers and figure objects. | |
62 | """ |
|
62 | """ | |
63 | if not figs: |
|
63 | if not figs: | |
64 | show(close=False) |
|
64 | show(close=False) | |
65 | else: |
|
65 | else: | |
66 | fig_managers = Gcf.get_all_fig_managers() |
|
66 | fig_managers = Gcf.get_all_fig_managers() | |
67 | fig_index = dict( [(fm.canvas.figure, fm.canvas) for fm in fig_managers] |
|
67 | fig_index = dict( [(fm.canvas.figure, fm.canvas) for fm in fig_managers] | |
68 | + [ (fm.canvas.figure.number, fm.canvas) for fm in fig_managers] ) |
|
68 | + [ (fm.canvas.figure.number, fm.canvas) for fm in fig_managers] ) | |
69 |
|
69 | |||
70 | for fig in figs: |
|
70 | for fig in figs: | |
71 | canvas = fig_index.get(fig) |
|
71 | canvas = fig_index.get(fig) | |
72 | if canvas is None: |
|
72 | if canvas is None: | |
73 | print('Warning: figure %s not available.' % fig) |
|
73 | print('Warning: figure %s not available.' % fig) | |
74 | else: |
|
74 | else: | |
75 | send_svg_canvas(canvas) |
|
75 | send_svg_canvas(canvas) | |
76 |
|
76 | |||
77 |
|
77 | |||
78 | def send_svg_canvas(canvas): |
|
78 | def send_svg_canvas(canvas): | |
79 | """Draw the current canvas and send it as an SVG payload. |
|
79 | """Draw the current canvas and send it as an SVG payload. | |
80 | """ |
|
80 | """ | |
81 | # Set the background to white instead so it looks good on black. We store |
|
81 | # Set the background to white instead so it looks good on black. We store | |
82 | # the current values to restore them at the end. |
|
82 | # the current values to restore them at the end. | |
83 | fc = canvas.figure.get_facecolor() |
|
83 | fc = canvas.figure.get_facecolor() | |
84 | ec = canvas.figure.get_edgecolor() |
|
84 | ec = canvas.figure.get_edgecolor() | |
85 | canvas.figure.set_facecolor('white') |
|
85 | canvas.figure.set_facecolor('white') | |
86 | canvas.figure.set_edgecolor('white') |
|
86 | canvas.figure.set_edgecolor('white') | |
87 | try: |
|
87 | try: | |
88 | add_plot_payload('svg', svg_from_canvas(canvas)) |
|
88 | add_plot_payload('svg', svg_from_canvas(canvas)) | |
89 | finally: |
|
89 | finally: | |
90 | canvas.figure.set_facecolor(fc) |
|
90 | canvas.figure.set_facecolor(fc) | |
91 | canvas.figure.set_edgecolor(ec) |
|
91 | canvas.figure.set_edgecolor(ec) | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | def svg_from_canvas(canvas): |
|
94 | def svg_from_canvas(canvas): | |
95 | """ Return a string containing the SVG representation of a FigureCanvasSvg. |
|
95 | """ Return a string containing the SVG representation of a FigureCanvasSvg. | |
96 | """ |
|
96 | """ | |
97 | string_io = StringIO() |
|
97 | string_io = StringIO() | |
98 |
canvas.print_ |
|
98 | canvas.print_figure(string_io, format='svg') | |
99 | return string_io.getvalue() |
|
99 | return string_io.getvalue() | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def draw_if_interactive(): |
|
102 | def draw_if_interactive(): | |
103 | """ |
|
103 | """ | |
104 | Is called after every pylab drawing command |
|
104 | Is called after every pylab drawing command | |
105 | """ |
|
105 | """ | |
106 | # We simply flag we were called and otherwise do nothing. At the end of |
|
106 | # We simply flag we were called and otherwise do nothing. At the end of | |
107 | # the code execution, a separate call to show_close() will act upon this. |
|
107 | # the code execution, a separate call to show_close() will act upon this. | |
108 | show._draw_called = True |
|
108 | show._draw_called = True | |
109 |
|
109 | |||
110 |
|
110 | |||
111 | def flush_svg(): |
|
111 | def flush_svg(): | |
112 | """Call show, close all open figures, sending all SVG images. |
|
112 | """Call show, close all open figures, sending all SVG images. | |
113 |
|
113 | |||
114 | This is meant to be called automatically and will call show() if, during |
|
114 | This is meant to be called automatically and will call show() if, during | |
115 | prior code execution, there had been any calls to draw_if_interactive. |
|
115 | prior code execution, there had been any calls to draw_if_interactive. | |
116 | """ |
|
116 | """ | |
117 | if show._draw_called: |
|
117 | if show._draw_called: | |
118 | show(close=True) |
|
118 | show(close=True) | |
119 | show._draw_called = False |
|
119 | show._draw_called = False |
General Comments 0
You need to be logged in to leave comments.
Login now