Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the 'figure' function with a method in the Figure class #148

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions gmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,6 @@
kwargs_to_strings, launch_external_viewer, unique_name, worldwind_show


def figure(name):
"""
Start a new figure.
All plotting commands run afterward will append to this figure.
Unlike the command-line version (``gmt figure``), this function does not
trigger the generation of a figure file. An explicit call to
:func:`gmt.savefig` or :func:`gmt.psconvert` must be made in order to get a
file.
Parameters
----------
name : str
A unique name for this figure. Will use the name to refer to a
particular figure. You can come back to the figure by calling this
function with the same name as before.
"""
# Passing format '-' tells gmt.end to not produce any files.
fmt = '-'
with LibGMT() as lib:
lib.call_module('figure', '{} {}'.format(name, fmt))


class Figure(BasePlotting):
"""
A GMT figure to handle all plotting.
Expand Down Expand Up @@ -71,18 +46,35 @@ class Figure(BasePlotting):
def __init__(self):
self._name = unique_name()
self._preview_dir = TemporaryDirectory(prefix=self._name + '-preview-')
self._activate_figure()

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

def _activate_figure(self):
"""
Start and/or activate the current figure.
All plotting commands run afterward will append to this figure.
Unlike the command-line version (``gmt figure``), this method does not
trigger the generation of a figure file. An explicit call to
:meth:`gmt.Figure.savefig` or :meth:`gmt.Figure.psconvert` must be made
in order to get a file.
"""
# Passing format '-' tells gmt.end to not produce any files.
fmt = '-'
with LibGMT() as lib:
lib.call_module('figure', '{} {}'.format(self._name, fmt))

def _preprocess(self, **kwargs):
"""
Call the ``figure`` module before each plotting command to ensure we're
plotting to this particular figure.
"""
figure(self._name)
self._activate_figure()
return kwargs

@fmt_docstring
Expand Down