Skip to content

Commit 7466dc3

Browse files
willschlitzerseismanweiji14
authored
Wrap histogram (#1072)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 9fb1cec commit 7466dc3

File tree

6 files changed

+97
-0
lines changed

6 files changed

+97
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Plotting data and laying out the map:
2929
Figure.grdcontour
3030
Figure.grdimage
3131
Figure.grdview
32+
Figure.histogram
3233
Figure.image
3334
Figure.inset
3435
Figure.legend

pygmt/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ def _repr_html_(self):
411411
grdcontour,
412412
grdimage,
413413
grdview,
414+
histogram,
414415
image,
415416
inset,
416417
legend,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pygmt.src.grdinfo import grdinfo
1818
from pygmt.src.grdtrack import grdtrack
1919
from pygmt.src.grdview import grdview
20+
from pygmt.src.histogram import histogram
2021
from pygmt.src.image import image
2122
from pygmt.src.info import info
2223
from pygmt.src.inset import inset

pygmt/src/histogram.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Histogram - Create a histogram
3+
"""
4+
from pygmt.clib import Session
5+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
6+
7+
8+
@fmt_docstring
9+
@use_alias(
10+
A="horizontal",
11+
B="frame",
12+
C="cmap",
13+
G="fill",
14+
J="projection",
15+
R="region",
16+
T="series",
17+
W="pen",
18+
c="panel",
19+
l="label",
20+
p="perspective",
21+
)
22+
@kwargs_to_strings(R="sequence", T="sequence")
23+
def histogram(self, table, **kwargs):
24+
r"""
25+
Plots a histogram, and can read data from a file or
26+
list, array, or dataframe.
27+
28+
Full option list at :gmt-docs:`histogram.html`
29+
30+
{aliases}
31+
32+
Parameters
33+
----------
34+
table : str, list, or 1d array
35+
A data file name, list, or 1d numpy array. This is a required argument.
36+
{J}
37+
{R}
38+
{B}
39+
{CPT}
40+
{G}
41+
{W}
42+
{c}
43+
label : str
44+
Add a legend entry for the symbol or line being plotted.
45+
{p}
46+
horizontal : bool
47+
Plot the histogram using horizonal bars instead of the
48+
default vertical bars.
49+
series : int or str or list
50+
[*min*\ /*max*\ /]\ *inc*\ [**+n**\ ]
51+
Set the interval for the width of each bar in the histogram.
52+
53+
"""
54+
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
55+
with Session() as lib:
56+
file_context = lib.virtualfile_from_data(check_kind="vector", data=table)
57+
with file_context as infile:
58+
arg_str = " ".join([infile, build_arg_string(kwargs)])
59+
lib.call_module("histogram", arg_str)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 8499f1d0ef232ece53300c6aaf611607
3+
size: 10794
4+
path: test_histogram.png

pygmt/tests/test_histogram.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pylint: disable=redefined-outer-name
2+
"""
3+
Tests histogram.
4+
"""
5+
import pytest
6+
from pygmt import Figure
7+
8+
9+
@pytest.fixture(scope="module")
10+
def table():
11+
"""
12+
Returns a list of integers to be used in the histogram.
13+
"""
14+
return [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8]
15+
16+
17+
@pytest.mark.mpl_image_compare
18+
def test_histogram(table):
19+
"""
20+
Tests plotting a histogram using a list of integers.
21+
"""
22+
fig = Figure()
23+
fig.histogram(
24+
table=table,
25+
projection="X10c/10c",
26+
region=[0, 9, 0, 6],
27+
series=1,
28+
frame="a",
29+
fill="green",
30+
)
31+
return fig

0 commit comments

Comments
 (0)