def cumulative_detections(dates=None, template_names=None, detections=None,<EOL>plot_grouped=False, group_name=None, rate=False,<EOL>plot_legend=True, ax=None, **kwargs):
import matplotlib.pyplot as plt<EOL>from eqcorrscan.core.match_filter import Detection<EOL>colors = cycle(['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>linestyles = cycle(['<STR_LIT:->', '<STR_LIT>', '<STR_LIT>', '<STR_LIT::>'])<EOL>if not detections:<EOL><INDENT>if type(dates[<NUM_LIT:0>]) != list:<EOL><INDENT>dates = [dates]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dates = []<EOL>template_names = []<EOL>for detection in detections:<EOL><INDENT>if not type(detection) == Detection:<EOL><INDENT>raise IOError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>dates.append(detection.detect_time.datetime)<EOL>template_names.append(detection.template_name)<EOL><DEDENT>_dates = []<EOL>_template_names = []<EOL>for template_name in list(set(template_names)):<EOL><INDENT>_template_names.append(template_name)<EOL>_dates.append([date for i, date in enumerate(dates)<EOL>if template_names[i] == template_name])<EOL><DEDENT>dates = _dates<EOL>template_names = _template_names<EOL><DEDENT>if plot_grouped:<EOL><INDENT>_dates = []<EOL>for template_dates in dates:<EOL><INDENT>_dates += template_dates<EOL><DEDENT>dates = [_dates]<EOL>if group_name:<EOL><INDENT>template_names = group_name<EOL><DEDENT>else:<EOL><INDENT>template_names = ['<STR_LIT>']<EOL><DEDENT><DEDENT>if ax is None:<EOL><INDENT>fig = plt.figure()<EOL>ax = fig.add_subplot(<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>fig = ax.figure()<EOL><DEDENT>ax.margins(<NUM_LIT:0>, <NUM_LIT:0>)<EOL>min_date = min([min(_d) for _d in dates])<EOL>max_date = max([max(_d) for _d in dates])<EOL>for k, template_dates in enumerate(dates):<EOL><INDENT>template_dates.sort()<EOL>plot_dates = deepcopy(template_dates)<EOL>plot_dates.insert(<NUM_LIT:0>, min_date)<EOL>plot_dates.insert(-<NUM_LIT:1>, template_dates[-<NUM_LIT:1>])<EOL>color = next(colors)<EOL>if color == '<STR_LIT>':<EOL><INDENT>linestyle = next(linestyles)<EOL><DEDENT>counts = np.arange(-<NUM_LIT:1>, len(template_dates) + <NUM_LIT:1>)<EOL>if rate:<EOL><INDENT>if not plot_grouped:<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise NotImplementedError(msg)<EOL><DEDENT>if <NUM_LIT> < (max_date - min_date).days < <NUM_LIT>:<EOL><INDENT>bins = (max_date - min_date).days<EOL>ax.set_ylabel('<STR_LIT>')<EOL><DEDENT>elif (max_date - min_date).days <= <NUM_LIT>:<EOL><INDENT>bins = (max_date - min_date).days * <NUM_LIT:4><EOL>ax.set_ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>bins = (max_date - min_date).days // <NUM_LIT:7><EOL>ax.set_ylabel('<STR_LIT>')<EOL><DEDENT>if len(plot_dates) <= <NUM_LIT:10>:<EOL><INDENT>bins = <NUM_LIT:1><EOL><DEDENT>ax.hist(mdates.date2num(plot_dates), bins=bins,<EOL>label='<STR_LIT>', color='<STR_LIT>',<EOL>alpha=<NUM_LIT:0.5>)<EOL><DEDENT>else:<EOL><INDENT>ax.plot(plot_dates, counts, linestyle,<EOL>color=color, label=template_names[k],<EOL>linewidth=<NUM_LIT>, drawstyle='<STR_LIT>')<EOL>ax.set_ylabel('<STR_LIT>')<EOL><DEDENT><DEDENT>ax.set_xlabel('<STR_LIT>')<EOL>mins = mdates.MinuteLocator()<EOL>max_date = dates[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>min_date = max_date<EOL>for date_list in dates:<EOL><INDENT>if max(date_list) > max_date:<EOL><INDENT>max_date = max(date_list)<EOL><DEDENT>if min(date_list) < min_date:<EOL><INDENT>min_date = min(date_list)<EOL><DEDENT><DEDENT>timedif = max_date - min_date<EOL>if <NUM_LIT> <= timedif.total_seconds() <= <NUM_LIT>:<EOL><INDENT>hours = mdates.MinuteLocator(byminute=[<NUM_LIT:0>, <NUM_LIT:30>])<EOL>mins = mdates.MinuteLocator(byminute=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:10>))<EOL><DEDENT>elif <NUM_LIT> <= timedif.total_seconds() < <NUM_LIT>:<EOL><INDENT>hours = mdates.MinuteLocator(byminute=[<NUM_LIT:0>, <NUM_LIT:15>, <NUM_LIT:30>, <NUM_LIT>])<EOL>mins = mdates.MinuteLocator(byminute=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:5>))<EOL><DEDENT>elif timedif.total_seconds() <= <NUM_LIT>:<EOL><INDENT>hours = mdates.MinuteLocator(byminute=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:2>))<EOL>mins = mdates.MinuteLocator(byminute=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:0.5>))<EOL><DEDENT>elif <NUM_LIT> < timedif.total_seconds() <= <NUM_LIT>:<EOL><INDENT>hours = mdates.HourLocator(byhour=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:3>))<EOL>mins = mdates.HourLocator(byhour=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:1>))<EOL><DEDENT>elif <NUM_LIT> < timedif.total_seconds() <= <NUM_LIT>:<EOL><INDENT>hours = mdates.HourLocator(byhour=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:6>))<EOL>mins = mdates.HourLocator(byhour=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:1>))<EOL><DEDENT>elif timedif.total_seconds() > <NUM_LIT>:<EOL><INDENT>hours = mdates.AutoDateLocator()<EOL>mins = mdates.HourLocator(byhour=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:3>))<EOL><DEDENT>else:<EOL><INDENT>hours = mdates.MinuteLocator(byminute=np.arange(<NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:5>))<EOL><DEDENT>if timedif.total_seconds() < <NUM_LIT>:<EOL><INDENT>ax.xaxis.set_minor_locator(mins)<EOL>hrFMT = mdates.DateFormatter('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>hrFMT = mdates.DateFormatter('<STR_LIT>')<EOL><DEDENT>ax.xaxis.set_major_locator(hours)<EOL>ax.xaxis.set_major_formatter(hrFMT)<EOL>fig.autofmt_xdate()<EOL>locs, labels = plt.xticks()<EOL>plt.setp(labels, rotation=<NUM_LIT:15>)<EOL>if not rate:<EOL><INDENT>ax.set_ylim([<NUM_LIT:0>, max([len(_d) for _d in dates])])<EOL><DEDENT>if plot_legend:<EOL><INDENT>if ax.legend() is not None:<EOL><INDENT>leg = ax.legend(loc=<NUM_LIT:2>, prop={'<STR_LIT:size>': <NUM_LIT:8>}, ncol=<NUM_LIT:2>)<EOL>leg.get_frame().set_alpha(<NUM_LIT:0.5>)<EOL><DEDENT><DEDENT>fig = _finalise_figure(fig=fig, **kwargs)  <EOL>return fig<EOL>
Plot cumulative detections or detection rate in time.

Simple plotting function to take a list of either datetime objects or
:class:`eqcorrscan.core.match_filter.Detection` objects and plot
a cumulative detections list.  Can take dates as a list of lists and will
plot each list separately, e.g. if you have dates from more than one
template it will overlay them in different colours.

:type dates: list
:param dates: Must be a list of lists of datetime.datetime objects
:type template_names: list
:param template_names: List of the template names in order of the dates
:type detections: list
:param detections: List of :class:`eqcorrscan.core.match_filter.Detection`
:type plot_grouped: bool
:param plot_grouped:
    Plot detections for each template individually, or group them all
    together - set to False (plot template detections individually) by
    default.
:type rate: bool
:param rate:
    Whether or not to plot the rate of detection per day. Only works for
    plot_grouped=True
:type plot_legend: bool
:param plot_legend:
    Specify whether to plot legend of template names. Defaults to True.


:returns: :class:`matplotlib.figure.Figure`

.. note::
    Can either take lists of
    :class:`eqcorrscan.core.match_filter.Detection` objects directly, or
    two lists of dates and template names - either/or, not both.

.. rubric:: Example

>>> import datetime as dt
>>> import numpy as np
>>> from eqcorrscan.utils.plotting import cumulative_detections
>>> dates = []
>>> for i in range(3):
...     dates.append([dt.datetime(2012, 3, 26) + dt.timedelta(n)
...                   for n in np.random.randn(100)])
>>> cumulative_detections(dates, ['a', 'b', 'c'],
...                       show=True) # doctest: +SKIP

.. plot::

    import datetime as dt
    import numpy as np
    from eqcorrscan.utils.plotting import cumulative_detections
    dates = []
    for i in range(3):
        dates.append([dt.datetime(2012, 3, 26) + dt.timedelta(n)
                      for n in np.random.randn(100)])
    cumulative_detections(dates, ['a', 'b', 'c'], show=True)

.. rubric:: Example 2: Rate plotting

>>> import datetime as dt
>>> import numpy as np
>>> from eqcorrscan.utils.plotting import cumulative_detections
>>> dates = []
>>> for i in range(3):
...     dates.append([dt.datetime(2012, 3, 26) + dt.timedelta(n)
...                   for n in np.random.randn(100)])
>>> cumulative_detections(dates, ['a', 'b', 'c'], plot_grouped=True,
...                       rate=True, show=True) # doctest: +SKIP

.. plot::

    import datetime as dt
    import numpy as np
    from eqcorrscan.utils.plotting import cumulative_detections
    dates = []
    for i in range(3):
        dates.append([dt.datetime(2012, 3, 26) + dt.timedelta(n)
                      for n in np.random.randn(100)])
    cumulative_detections(dates, ['a', 'b', 'c'], plot_grouped=True,
                          rate=True, show=True)