Skip to content

Commit 5e69285

Browse files
authored
Replace the 'figure' function with a method (#148)
The function activates a figure given a name. It's only used inside the Figure class, so it's better as a private method. Also, it's now called upon Figure creation (in __init__) to guarantee that a postscript stub and a new figure entry is generated in the GMT tables.
1 parent 86a2263 commit 5e69285

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

gmt/figure.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,6 @@
1818
kwargs_to_strings, launch_external_viewer, unique_name, worldwind_show
1919

2020

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-
4621
class Figure(BasePlotting):
4722
"""
4823
A GMT figure to handle all plotting.
@@ -71,18 +46,35 @@ class Figure(BasePlotting):
7146
def __init__(self):
7247
self._name = unique_name()
7348
self._preview_dir = TemporaryDirectory(prefix=self._name + '-preview-')
49+
self._activate_figure()
7450

7551
def __del__(self):
7652
# Clean up the temporary directory that stores the previews
7753
if hasattr(self, '_preview_dir'):
7854
self._preview_dir.cleanup()
7955

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+
8072
def _preprocess(self, **kwargs):
8173
"""
8274
Call the ``figure`` module before each plotting command to ensure we're
8375
plotting to this particular figure.
8476
"""
85-
figure(self._name)
77+
self._activate_figure()
8678
return kwargs
8779

8880
@fmt_docstring

0 commit comments

Comments
 (0)