Skip to content

Instantly share code, notes, and snippets.

@Stefan-Code
Created June 30, 2018 12:34
Show Gist options
  • Save Stefan-Code/2e62a299575b25f119a150db2c4a03d1 to your computer and use it in GitHub Desktop.
Save Stefan-Code/2e62a299575b25f119a150db2c4a03d1 to your computer and use it in GitHub Desktop.
# creates a LaTeX representation of the math for a scipy linregress straight line fit object
def fit_latex(fit, precision=2, xname='x', yname='y', xunit='', yunit='', stats_precision=4, show_R2=True, show_standard_error=True):
return '${y}={slope}{x}{unit_conversion} {sign} {intercept}{yunit}${stats}'.format(slope=round(fit.slope, precision),
intercept=round(abs(fit.intercept), precision),
x=xname,
y=yname,
sign='+' if fit.intercept >= 0 else '-',
yunit=r'\,\mathrm{{{}}}'.format(yunit) if yunit else '',
unit_conversion=r'\,\frac{{\mathrm{{{yunit}}}}}{{\mathrm{{{xunit}}}}}'.format(yunit=yunit, xunit=xunit) if xunit or yunit else '',
stats=r'\quad ({R2}{sep}{S})'.format(
R2='$R^2 = {R2}$'.format(R2=round(fit.rvalue, stats_precision),) if show_R2 else '',
S=r'$\sigma_\mathrm{{est}}={S}$'.format(S=round(fit.stderr, stats_precision)) if show_standard_error else '',
sep=r',\ ' if show_R2 and show_standard_error else ''
) if show_R2 or show_standard_error else '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment