Skip to content

Commit 98ca37c

Browse files
authored
Sphinx extension for GMT/Python plots (#208)
Add a sphinx extension `gmt.sphinxext.gmtplot` to the package. It enables the `gmt-plot` directive that executes the given code, captures the output of `gmt.Figure.show` and stdout, and inserts them into the rst document. Includes a plot in the front page and a page documenting the extension. It also serves as a test for the extension. The code is not tested with pytest. This could possibly be done using [sphinx-testing](https://github.com/sphinx-doc/sphinx-testing). Fixes #129
1 parent 2cd454c commit 98ca37c

File tree

11 files changed

+501
-115
lines changed

11 files changed

+501
-115
lines changed

.github/readme-example.png

668 KB
Loading

README.rst

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,38 @@
1414
</p>
1515

1616
<p align="center">
17+
<a href="http://try.gmtpython.xyz/">Online Demo</a> |
1718
<a href="https://www.gmtpython.xyz">Documentation</a> |
18-
<a href="https://www.gmtpython.xyz/latest/install.html">Install</a> |
19-
<a href="https://www.gmtpython.xyz/latest/tutorials">Tutorials</a> |
20-
<a href="https://www.gmtpython.xyz/latest/api">API</a> |
19+
<a href="https://github.com/GenericMappingTools/gmt-python">Source Code</a> |
2120
<a href="https://gitter.im/GenericMappingTools/gmt-python">Contact</a>
2221
</p>
2322

2423

24+
.. code:: python
25+
26+
import gmt
27+
28+
# Load sample earthquake data in a pandas.DataFrame
29+
quakes = gmt.datasets.load_usgs_quakes()
30+
31+
# Load the builtin Earth relief grid as an xarray.DataArray.
32+
relief = gmt.datasets.load_earth_relief(resolution="30m")
33+
34+
# The Figure object controls all plotting functions
35+
fig = gmt.Figure()
36+
# Setup a map with a global region, a Mollweide projection, and automatic ticks
37+
fig.basemap(region="g", projection="W200/8i", frame=True)
38+
# Plot the Earth relief grid in pseudo-color.
39+
fig.grdimage(relief, cmap="geo")
40+
# Plot earthquakes as circles. Size maps to magnitude and color to depth.
41+
fig.plot(x=quakes.longitude, y=quakes.latitude, sizes=0.01*2**quakes.mag,
42+
color=quakes.depth/quakes.depth.max(), cmap="viridis", style="cc")
43+
# Show a preview of the image (inline if in a Jupyter notebook).
44+
fig.show()
45+
46+
.. image:: .github/readme-example.png
47+
48+
2549
Disclaimer
2650
----------
2751

@@ -39,52 +63,35 @@ Getting started
3963
---------------
4064

4165
1. Try an online demo at `try.gmtpython.xyz <http://try.gmtpython.xyz>`__
42-
2. `Install <https://www.gmtpython.xyz/latest/install.html>`__ (tested and working on
43-
Linux and OSX)
66+
2. `Install <https://www.gmtpython.xyz/latest/install.html>`__ (Linux and Mac)
4467
3. Follow the `Tutorials <https://www.gmtpython.xyz/latest/tutorials>`__.
45-
4. Take a look at the `API <https://www.gmtpython.xyz/latest/api>`__ for a list of
46-
modules that are already available.
47-
48-
49-
About
50-
-----
51-
52-
Watch the `Scipy 2017 talk about GMT/Python <https://github.com/GenericMappingTools/scipy2017>`__
53-
for more information about the history and goals of the project:
54-
55-
.. image:: https://raw.githubusercontent.com/GenericMappingTools/gmt-python/master/doc/_static/scipy2017-youtube-thumbnail.png
56-
:alt: YouTube recording of the Scipy2017 talk.
57-
:target: https://www.youtube.com/watch?v=93M4How7R24
68+
4. Take a look at the `API <https://www.gmtpython.xyz/latest/api>`__ to see what is
69+
available.
5870

5971

6072
Project goals
6173
-------------
6274

63-
* Build a modern Pythonic API that appeals to Python programmers who want to
64-
use GMT.
65-
* Implement readable and explicit aliases for the GMT command-line arguments
66-
(``region`` instead of ``R``, ``projection`` instead of ``J``, etc).
67-
* Use the new `GMT modern mode
68-
<http://gmt.soest.hawaii.edu/projects/gmt/wiki/Modernization>`__ for
69-
simplified execution and figure generation.
70-
* Interface with the GMT C API directly using
71-
`ctypes <https://docs.python.org/3/library/ctypes.html>`__ (no system calls).
72-
* Integration with the `Jupyter notebook <http://jupyter.org/>`__ to display
73-
plots and maps inline.
74-
* Input and output using Python native containers: numpy ``ndarray`` or pandas
75-
``DataFrame`` for data tables and `xarray <http://xarray.pydata.org>`__
76-
``Dataset`` for netCDF grids.
75+
* Make GMT more accessible to new users.
76+
* Build a Pythonic API for GMT.
77+
* Interface with the GMT C API directly using ctypes (no system calls).
78+
* Support for rich display in the Jupyter notebook.
79+
* Integration with the Scipy stack: numpy.ndarray or pandas.DataFrame for data tables
80+
and xarray.DataArray for grids.
7781

7882

7983
Contacting Us
8084
-------------
8185

82-
* Most discussion happens `on Github <https://github.com/GenericMappingTools/gmt-python>`__.
83-
Feel free to `open an issue
84-
<https://github.com/GenericMappingTools/gmt-python/issues/new>`__ or comment
85-
on any open issue or pull request.
86-
* We have `chat room on Gitter <https://gitter.im/GenericMappingTools/gmt-python>`__
86+
* Most discussion happens `on Github
87+
<https://github.com/GenericMappingTools/gmt-python>`__. Feel free to `open an issue
88+
<https://github.com/GenericMappingTools/gmt-python/issues/new>`__ or comment on any
89+
open issue or pull request.
90+
* We have a `chat room on Gitter <https://gitter.im/GenericMappingTools/gmt-python>`__
8791
where you can ask questions and leave comments.
92+
* This project is released with a `Contributor Code of Conduct
93+
<https://github.com/GenericMappingTools/gmt-python/blob/master/CODE_OF_CONDUCT.md>`__.
94+
By participating in this project you agree to abide by its terms.
8895

8996

9097
Contributing
@@ -93,40 +100,36 @@ Contributing
93100
Code of conduct
94101
+++++++++++++++
95102

96-
Please note that this project is released with a
97-
`Contributor Code of Conduct <https://github.com/GenericMappingTools/gmt-python/blob/master/CODE_OF_CONDUCT.md>`__.
103+
Please note that this project is released with a `Contributor Code of Conduct
104+
<https://github.com/GenericMappingTools/gmt-python/blob/master/CODE_OF_CONDUCT.md>`__.
98105
By participating in this project you agree to abide by its terms.
99106

100107
Contributing Guidelines
101108
+++++++++++++++++++++++
102109

103-
Please read our
104-
`Contributing Guide <https://github.com/GenericMappingTools/gmt-python/blob/master/CONTRIBUTING.md>`__
105-
to see how you can help and give feedback.
110+
Please read our `Contributing Guide
111+
<https://github.com/GenericMappingTools/gmt-python/blob/master/CONTRIBUTING.md>`__ to
112+
see how you can help and give feedback.
106113

107114
Imposter syndrome disclaimer
108115
++++++++++++++++++++++++++++
109116

110117
**We want your help.** No, really.
111118

112-
There may be a little voice inside your head that is telling you that you're
113-
not ready to be an open source contributor; that your skills aren't nearly good
114-
enough to contribute.
115-
What could you possibly offer?
119+
There may be a little voice inside your head that is telling you that you're not ready
120+
to be an open source contributor; that your skills aren't nearly good enough to
121+
contribute. What could you possibly offer?
116122

117123
We assure you that the little voice in your head is wrong.
118124

119125
**Being a contributor doesn't just mean writing code**.
120-
Equality important contributions include:
121-
writing or proof-reading documentation, suggesting or implementing tests, or
122-
even giving feedback about the project (including giving feedback about the
123-
contribution process).
124-
If you're coming to the project with fresh eyes, you might see the errors and
125-
assumptions that seasoned contributors have glossed over.
126-
If you can write any code at all, you can contribute code to open source.
127-
We are constantly trying out new skills, making mistakes, and learning from
128-
those mistakes.
129-
That's how we all improve and we are happy to help others learn.
126+
Equality important contributions include: writing or proof-reading documentation,
127+
suggesting or implementing tests, or even giving feedback about the project (including
128+
giving feedback about the contribution process). If you're coming to the project with
129+
fresh eyes, you might see the errors and assumptions that seasoned contributors have
130+
glossed over. If you can write any code at all, you can contribute code to open source.
131+
We are constantly trying out new skills, making mistakes, and learning from those
132+
mistakes. That's how we all improve and we are happy to help others learn.
130133

131134
*This disclaimer was adapted from the*
132135
`MetPy project <https://github.com/Unidata/MetPy>`__.
@@ -135,24 +138,19 @@ That's how we all improve and we are happy to help others learn.
135138
Related projects
136139
----------------
137140

138-
* `GMT.jl <https://github.com/GenericMappingTools/GMT.jl>`__ -- A Julia wrapper
141+
* `GMT.jl <https://github.com/GenericMappingTools/GMT.jl>`__: A Julia wrapper for GMT.
142+
* `gmtmex <https://github.com/GenericMappingTools/GMT.jl>`__: A Matlab/Octave wrapper
139143
for GMT.
140-
* `gmtmex <https://github.com/GenericMappingTools/GMT.jl>`__ -- A Matlab/Octave
141-
wrapper for GMT.
142144

143145
Other Python wrappers for GMT:
144146

145-
* `gmtpy <https://github.com/emolch/gmtpy>`__ by
146-
`Sebastian Heimann <https://github.com/emolch>`__
147-
* `pygmt <https://github.com/ian-r-rose/pygmt>`__ by
148-
`Ian Rose <https://github.com/ian-r-rose>`__
149-
* `PyGMT <https://github.com/glimmer-cism/PyGMT>`__ by
150-
`Magnus Hagdorn <https://github.com/mhagdorn>`__
147+
* `gmtpy <https://github.com/emolch/gmtpy>`__ by `Sebastian Heimann <https://github.com/emolch>`__
148+
* `pygmt <https://github.com/ian-r-rose/pygmt>`__ by `Ian Rose <https://github.com/ian-r-rose>`__
149+
* `PyGMT <https://github.com/glimmer-cism/PyGMT>`__ by `Magnus Hagdorn <https://github.com/mhagdorn>`__
151150

152151

153152
License
154153
-------
155154

156-
gmt-python is free software: you can redistribute it and/or modify it under the
157-
terms of the **BSD 3-clause License**. A copy of this license is provided in
158-
``LICENSE.txt``.
155+
GMT/Python is free software: you can redistribute it and/or modify it under the terms of
156+
the **BSD 3-clause License**. A copy of this license is provided in ``LICENSE.txt``.

doc/_static/front-page-example.png

-167 KB
Binary file not shown.

doc/_static/style.css

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ h1 {
1515
font-size: 200%;
1616
}
1717

18+
.gmtplot-output {
19+
width: 100%;
20+
overflow: auto;
21+
}
22+
23+
.gmtplot-output img {
24+
max-width: 700px;
25+
width: 100%;
26+
}
27+
28+
.gmtplot-output pre {
29+
color: #888888;
30+
}
31+
32+
.gmtplot-output-figure {
33+
margin-bottom: 50px;
34+
}
35+
36+
.rst-content img {
37+
max-width: 700px;
38+
width: 100%;
39+
}
40+
1841
.sidebar-title {
1942
margin-top: 10px;
2043
margin-bottom: 0px;
@@ -57,8 +80,9 @@ h1 {
5780

5881
.wy-menu-vertical header, .wy-menu-vertical p.caption {
5982
font-size: 90%;
60-
font-weight: normal;
83+
font-weight: bold;
6184
color: #eeeeee;
85+
letter-spacing: 0.12em;
6286
}
6387

6488
.wy-nav-content {
@@ -79,11 +103,6 @@ h1 {
79103
font-size: 14px;
80104
}
81105

82-
.rst-content img {
83-
max-width: 450px;
84-
width: 100%;
85-
}
86-
87106
.source-link {
88107
float: right;
89108
}

doc/conf.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
'sphinx.ext.doctest',
1919
'sphinx.ext.viewcode',
2020
'sphinx.ext.extlinks',
21-
'sphinx.ext.intersphinx',
2221
'numpydoc',
2322
'nbsphinx',
23+
'gmt.sphinxext.gmtplot',
2424
]
2525

2626
# Autosummary pages will be generated by sphinx-autogen instead of sphinx-build
@@ -83,13 +83,6 @@
8383
'github_version': 'master',
8484
}
8585

86-
# intersphinx
87-
intersphinx_mapping = {
88-
'python': ('https://docs.python.org/3/', None),
89-
'numpy': ('https://docs.scipy.org/doc/numpy/', None),
90-
'pandas': ('http://pandas.pydata.org/pandas-docs/stable/', None),
91-
}
92-
9386
# Load the custom CSS files (needs sphinx >= 1.6 for this to work)
9487
def setup(app):
9588
app.add_stylesheet("style.css")

0 commit comments

Comments
 (0)