def freq_mag(magnitudes, completeness, max_mag, binsize=<NUM_LIT>, **kwargs):
import matplotlib.pyplot as plt<EOL>magnitudes.sort()<EOL>if np.isnan(magnitudes).any():<EOL><INDENT>warnings.warn('<STR_LIT>')<EOL>magnitudes = [mag for mag in magnitudes if not np.isnan(mag)]<EOL><DEDENT>if np.isinf(magnitudes).any():<EOL><INDENT>warnings.warn('<STR_LIT>')<EOL>magnitudes = [mag for mag in magnitudes if not np.isinf(mag)]<EOL><DEDENT>fig, ax1 = plt.subplots()<EOL>bins = np.arange(int(min(magnitudes) - <NUM_LIT:1>), int(max(magnitudes) + <NUM_LIT:1>),<EOL>binsize)<EOL>n, bins, patches = ax1.hist(magnitudes, bins, facecolor='<STR_LIT>',<EOL>alpha=<NUM_LIT:0.5>, label='<STR_LIT>')<EOL>ax1.set_ylabel('<STR_LIT>')<EOL>ax1.set_ylim([<NUM_LIT:0>, max(n) + <NUM_LIT:0.5> * max(n)])<EOL>plt.xlabel('<STR_LIT>')<EOL>counts = Counter(magnitudes)<EOL>cdf = np.zeros(len(counts))<EOL>mag_steps = np.zeros(len(counts))<EOL>for i, magnitude in enumerate(sorted(counts.keys(), reverse=True)):<EOL><INDENT>mag_steps[i] = magnitude<EOL>if i > <NUM_LIT:0>:<EOL><INDENT>cdf[i] = cdf[i - <NUM_LIT:1>] + counts[magnitude]<EOL><DEDENT>else:<EOL><INDENT>cdf[i] = counts[magnitude]<EOL><DEDENT><DEDENT>ax2 = ax1.twinx()<EOL>ax2.scatter(mag_steps, np.log10(cdf), c='<STR_LIT:k>', marker='<STR_LIT:+>', s=<NUM_LIT:20>, lw=<NUM_LIT:2>,<EOL>label='<STR_LIT>')<EOL>x = []<EOL>y = []<EOL>for i, magnitude in enumerate(mag_steps):<EOL><INDENT>if magnitude >= completeness <= max_mag:<EOL><INDENT>x.append(magnitude)<EOL>y.append(cdf[i])<EOL><DEDENT><DEDENT>fit = np.polyfit(x, np.log10(y), <NUM_LIT:1>)<EOL>fit_fn = np.poly1d(fit)<EOL>ax2.plot(magnitudes, fit_fn(magnitudes), '<STR_LIT>',<EOL>label='<STR_LIT>' + str(abs(fit[<NUM_LIT:0>]))[<NUM_LIT:0>:<NUM_LIT:4>] +<EOL>'<STR_LIT>' + str(completeness))<EOL>ax2.set_ylabel('<STR_LIT>')<EOL>plt.xlim([min(magnitudes) - <NUM_LIT:0.1>, max(magnitudes) + <NUM_LIT>])<EOL>plt.ylim([min(np.log10(cdf)) - <NUM_LIT:0.5>, max(np.log10(cdf)) + <NUM_LIT:1.0>])<EOL>plt.legend(loc=<NUM_LIT:2>)<EOL>fig = _finalise_figure(fig=fig, **kwargs)  <EOL>return fig<EOL>
Plot a frequency-magnitude histogram and cumulative density plot.

Currently this will compute a b-value, for a given completeness.
B-value is computed by linear fitting to section of curve between
completeness and max_mag.

:type magnitudes: list
:param magnitudes: list of float of magnitudes
:type completeness: float
:param completeness: Level to compute the b-value above
:type max_mag: float
:param max_mag: Maximum magnitude to try and fit a b-value to
:type binsize: float
:param binsize: Width of histogram bins, defaults to 0.2

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

.. Note::
    See :func:`eqcorrscan.utils.mag_calc.calc_b_value` for a least-squares
    method of estimating completeness and b-value. For estimating maximum
    curvature see :func:`eqcorrscan.utils.mag_calc.calc_max_curv`.

.. rubric:: Example

>>> from obspy.clients.fdsn import Client
>>> from obspy import UTCDateTime
>>> from eqcorrscan.utils.plotting import freq_mag
>>> client = Client('IRIS')
>>> t1 = UTCDateTime('2012-03-26T00:00:00')
>>> t2 = t1 + (3 * 86400)
>>> catalog = client.get_events(starttime=t1, endtime=t2, minmagnitude=3)
>>> magnitudes = [event.preferred_magnitude().mag for event in catalog]
>>> freq_mag(magnitudes, completeness=4, max_mag=7) # doctest: +SKIP

.. plot::

    from obspy.clients.fdsn import Client
    from obspy import UTCDateTime
    from eqcorrscan.utils.plotting import freq_mag
    client = Client('IRIS')
    t1 = UTCDateTime('2012-03-26T00:00:00')
    t2 = t1 + (3 * 86400)
    catalog = client.get_events(starttime=t1, endtime=t2, minmagnitude=3)
    magnitudes = [event.preferred_magnitude().mag for event in catalog]
    freq_mag(magnitudes, completeness=4, max_mag=7)