Skip to content

pysmo.tools.plotutils #

Utilities for plotting with pysmo types.

Pysmo provides functions that perform common operations on the types of data that match pysmo's types.

Functions:

plotseis #

plotseis(
    *seismograms: Seismogram,
    outfile: str = "",
    showfig: bool = True,
    title: str = "",
    **kwargs: dict
) -> Figure

Plot Seismogram objects.

Parameters:

  • seismograms (Seismogram, default: () ) –

    One or more seismogram objects. If a 'label' attribute is found it will be used to label the trace in the plot.

  • outfile (str, default: '' ) –

    Optionally save figure to this filename.

  • showfig (bool, default: True ) –

    Display figure.

  • title (str, default: '' ) –

    Optionally set figure title.

  • kwargs (dict, default: {} ) –

    Optionally add kwargs to pass to the plot command

Examples:

>>> from pysmo.classes import SAC
>>> from pysmo.tools.plotutils import plotseis
>>> seis = SAC.from_file("example.sac").seismogram
>>> fig = plotseis(seis)
>>>
Source code in pysmo/tools/plotutils.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def plotseis(
    *seismograms: Seismogram,
    outfile: str = "",
    showfig: bool = True,
    title: str = "",
    **kwargs: dict,
) -> matplotlib.figure.Figure:
    """Plot Seismogram objects.

    Parameters:
        seismograms: One or more seismogram objects. If a 'label' attribute is found
                     it will be used to label the trace in the plot.
        outfile: Optionally save figure to this filename.
        showfig: Display figure.
        title: Optionally set figure title.
        kwargs: Optionally add kwargs to pass to the plot command

    Examples:
        ```python
        >>> from pysmo.classes import SAC
        >>> from pysmo.tools.plotutils import plotseis
        >>> seis = SAC.from_file("example.sac").seismogram
        >>> fig = plotseis(seis)
        >>>
        ```
    """
    fig = plt.figure()
    for seis in seismograms:
        time = time_array(seis)
        plt.plot(time, seis.data, scalex=True, scaley=True, **kwargs)
    plt.xlabel("Time")
    plt.gcf().autofmt_xdate()
    fmt = mdates.DateFormatter("%H:%M:%S")
    plt.gca().xaxis.set_major_formatter(fmt)
    if not title:
        left, _ = plt.xlim()
        title = mdates.num2date(left).strftime("%Y-%m-%d %H:%M:%S")
    plt.title(title)
    if outfile:
        plt.savefig(outfile)
    if showfig:
        plt.show()
    return fig

time_array #

time_array(seismogram: Seismogram) -> NDArray

Create an array containing Matplotlib dates.

Parameters:

Returns:

  • NDArray

    Array containing the Matplotlib dates of seismogram data.

  • NDArray

    array containing the Matplotlib dates (number of days since 1970)

  • NDArray

    of each point in the seismogram data.

Examples:

>>> from pysmo.tools.plotutils import time_array
>>> from pysmo.classes import SAC
>>> seis = SAC.from_file("example.sac").seismogram
>>> seis_data = seis.data
>>> seis_times = time_array(seis)
>>> for t, v in zip(seis_times, seis_data):
...     print(t,v)
...
12843.30766388889 2302.0
12843.307664120372 2313.0
12843.307664351854 2345.0
12843.307664583335 2377.0
...
>>>
Source code in pysmo/tools/plotutils.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def time_array(seismogram: Seismogram) -> npt.NDArray:
    """Create an array containing Matplotlib dates.

    Parameters:
        seismogram: Seismogram object.

    Returns:
        Array containing the Matplotlib dates of seismogram data.
        array containing the Matplotlib dates (number of days since 1970)
        of each point in the seismogram data.

    Examples:
        ```python
        >>> from pysmo.tools.plotutils import time_array
        >>> from pysmo.classes import SAC
        >>> seis = SAC.from_file("example.sac").seismogram
        >>> seis_data = seis.data
        >>> seis_times = time_array(seis)
        >>> for t, v in zip(seis_times, seis_data):
        ...     print(t,v)
        ...
        12843.30766388889 2302.0
        12843.307664120372 2313.0
        12843.307664351854 2345.0
        12843.307664583335 2377.0
        ...
        >>>
        ```
    """
    start = mdates.date2num(seismogram.begin_time)
    end = mdates.date2num(seismogram.end_time)
    return np.linspace(start, end, len(seismogram))

unix_time_array #

unix_time_array(seismogram: Seismogram) -> NDArray

Create an array containing unix epoch dates.

Parameters:

Returns:

  • NDArray

    array containing the unix epoch times (number of seconds since 1970)

  • NDArray

    of each point in the seismogram data.

Examples:

>>> from pysmo.classes import SAC
>>> from pysmo.tools.plotutils import unix_time_array
>>> seis = SAC.from_file("example.sac").seismogram
>>> seis_data = seis.data
>>> seis_times = unix_time_array(seis)
>>> for t, v in zip(seis_times, seis_data):
...     print(t,v)
...
1109661782.16 2302.0
1109661782.18 2313.0
1109661782.2 2345.0
1109661782.22 2377.0
...
>>>
Source code in pysmo/tools/plotutils.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def unix_time_array(seismogram: Seismogram) -> npt.NDArray:
    """Create an array containing unix epoch dates.

    Parameters:
        seismogram: Seismogram object.

    Returns:
        array containing the unix epoch times (number of seconds since 1970)
        of each point in the seismogram data.

    Examples:
        ```python
        >>> from pysmo.classes import SAC
        >>> from pysmo.tools.plotutils import unix_time_array
        >>> seis = SAC.from_file("example.sac").seismogram
        >>> seis_data = seis.data
        >>> seis_times = unix_time_array(seis)
        >>> for t, v in zip(seis_times, seis_data):
        ...     print(t,v)
        ...
        1109661782.16 2302.0
        1109661782.18 2313.0
        1109661782.2 2345.0
        1109661782.22 2377.0
        ...
        >>>
        ```
    """
    start = seismogram.begin_time.timestamp()
    end = seismogram.end_time.timestamp()
    return np.linspace(start, end, len(seismogram))