# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Figure 4 - SEL versus biophony

# #### import statements

import pandas
import numpy
# %matplotlib inline
from matplotlib import pyplot
from matplotlib.lines import Line2D
import seaborn
from scipy import stats

from matplotlib import rcParams

# #### variable definitions

data_filepath = ""

# #### formating

seaborn_blue = seaborn.color_palette()[0]
seaborn.set()

rcParams['font.sans-serif']

rcParams['font.sans-serif'] = ['Helvetica Neue',
                               'Arial',
                               'Bitstream Vera Sans',
                               'DejaVu Sans',
                               'Lucida Grande',
                               'Verdana',
                               'Geneva',
                               'Lucid',
                               'Avant Garde',
                               'sans-serif']

# #### sort data

data = pandas.read_csv(data_filepath)
data = data.loc[data.site<=30]

data = data.sort_values(by=['site', 'sound']).reset_index(drop=True)

# #### compute means

means = data[['site', 'sel', 'biophony']].groupby('site').mean()
mins = data[['site', 'sel', 'biophony']].groupby('site').min()
maxs = data[['site', 'sel', 'biophony']].groupby('site').max()

# #### plot

# +
figure4, ax = pyplot.subplots()
figure4.subplots_adjust(left=0.15, bottom=0.15, right=0.95, top=0.95, wspace=0, hspace=0)
figure4.set_figheight(3.30)
figure4.set_figwidth(3.30)

p1 = ax.scatter('sel', 'biophony', data=means, color='black')
xl = ax.set_xlabel('SEL (decibels)')
yl = ax.set_ylabel('percent biophony')
lx = ax.set_xlim(-60, -30)
ly = ax.set_ylim(0, 15)

yt = ax.set_yticks(numpy.arange(1, 17, 2))

for s in means.index:
    x = means.loc[s].sel
    y1 = mins.loc[s].biophony
    y2 = maxs.loc[s].biophony
    ax.add_line(Line2D([x, x], [y1, y2], color='black', linewidth=1, alpha=0.5))

# legend
ap = dict(arrowstyle='-',
          connectionstyle='arc3,rad=0.3', linestyle=':', linewidth=0.5, color='gray')
a1 = ax.annotate('site mean', (means.loc[29].sel, means.loc[29].biophony), xytext=(-38, 5), arrowprops=ap)
a2 = ax.annotate('site max', (means.loc[29].sel, maxs.loc[29].biophony), xytext=(-38, 6), arrowprops=ap)
a3 = ax.annotate('site min', (means.loc[29].sel, mins.loc[29].biophony), xytext=(-38, 4), arrowprops=ap)

# pearsonr
r = stats.pearsonr(x=means.sel, y=means.biophony)
a4 = ax.text(x=-31, y=14, s='pearsonr = {0:.2f}, p = {1:.1e}'.format(r[0], r[1]), ha='right', va='center')
# -

# #### save figure

# +
#figure4.savefig("", dpi=600)

StarcoderdataJupyterScriptsDedupFiltered