# 5.8.1.3. eqcorrscan.utils.mag_calc.calc_b_value

`eqcorrscan.utils.mag_calc.``calc_b_value`(magnitudes, completeness, max_mag=None, plotvar=True)

  Calculate the b-value for a range of completeness magnitudes.

  Calculates a power-law fit to given magnitudes for each completeness magnitude. Plots the b-values and residuals for the fitted catalogue against the completeness values. Computes fits using numpy.polyfit, which uses a least-squares technique.

  Parameters: | magnitudes (list) Magnitudes to compute the b-value for. completeness (list) list of completeness values to compute b-values for. max_mag (float) Maximum magnitude to attempt to fit in magnitudes. plotvar (bool) Turn plotting on or off.
  Return type: | list
  Returns: | List of tuples of (completeness, b-value, residual, number of magnitudes used)

  Example

      >>> from obspy.clients.fdsn import Client
      >>> from obspy import UTCDateTime
      >>> from eqcorrscan.utils.mag_calc import calc_b_value
      >>> 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.magnitudes[0].mag for event in catalog]
      >>> b_values = calc_b_value(magnitudes, completeness=np.arange(3, 7, 0.2),
      ...                         plotvar=False)
      >>> round(b_values[4][1])
      1.0
      >>> # We can set a maximum magnitude:
      >>> b_values = calc_b_value(magnitudes, completeness=np.arange(3, 7, 0.2),
      ...                         plotvar=False, max_mag=5)
      >>> round(b_values[4][1])
      1.0