|
18 | 18 | kwargs_to_strings, launch_external_viewer, unique_name, worldwind_show |
19 | 19 |
|
20 | 20 |
|
21 | | -def figure(name): |
22 | | - """ |
23 | | - Start a new figure. |
24 | | -
|
25 | | - All plotting commands run afterward will append to this figure. |
26 | | -
|
27 | | - Unlike the command-line version (``gmt figure``), this function does not |
28 | | - trigger the generation of a figure file. An explicit call to |
29 | | - :func:`gmt.savefig` or :func:`gmt.psconvert` must be made in order to get a |
30 | | - file. |
31 | | -
|
32 | | - Parameters |
33 | | - ---------- |
34 | | - name : str |
35 | | - A unique name for this figure. Will use the name to refer to a |
36 | | - particular figure. You can come back to the figure by calling this |
37 | | - function with the same name as before. |
38 | | -
|
39 | | - """ |
40 | | - # Passing format '-' tells gmt.end to not produce any files. |
41 | | - fmt = '-' |
42 | | - with LibGMT() as lib: |
43 | | - lib.call_module('figure', '{} {}'.format(name, fmt)) |
44 | | - |
45 | | - |
46 | 21 | class Figure(BasePlotting): |
47 | 22 | """ |
48 | 23 | A GMT figure to handle all plotting. |
@@ -71,18 +46,35 @@ class Figure(BasePlotting): |
71 | 46 | def __init__(self): |
72 | 47 | self._name = unique_name() |
73 | 48 | self._preview_dir = TemporaryDirectory(prefix=self._name + '-preview-') |
| 49 | + self._activate_figure() |
74 | 50 |
|
75 | 51 | def __del__(self): |
76 | 52 | # Clean up the temporary directory that stores the previews |
77 | 53 | if hasattr(self, '_preview_dir'): |
78 | 54 | self._preview_dir.cleanup() |
79 | 55 |
|
| 56 | + def _activate_figure(self): |
| 57 | + """ |
| 58 | + Start and/or activate the current figure. |
| 59 | +
|
| 60 | + All plotting commands run afterward will append to this figure. |
| 61 | +
|
| 62 | + Unlike the command-line version (``gmt figure``), this method does not |
| 63 | + trigger the generation of a figure file. An explicit call to |
| 64 | + :meth:`gmt.Figure.savefig` or :meth:`gmt.Figure.psconvert` must be made |
| 65 | + in order to get a file. |
| 66 | + """ |
| 67 | + # Passing format '-' tells gmt.end to not produce any files. |
| 68 | + fmt = '-' |
| 69 | + with LibGMT() as lib: |
| 70 | + lib.call_module('figure', '{} {}'.format(self._name, fmt)) |
| 71 | + |
80 | 72 | def _preprocess(self, **kwargs): |
81 | 73 | """ |
82 | 74 | Call the ``figure`` module before each plotting command to ensure we're |
83 | 75 | plotting to this particular figure. |
84 | 76 | """ |
85 | | - figure(self._name) |
| 77 | + self._activate_figure() |
86 | 78 | return kwargs |
87 | 79 |
|
88 | 80 | @fmt_docstring |
|
0 commit comments