-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_figS34.py
172 lines (121 loc) · 4.41 KB
/
make_figS34.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
# coding: utf-8
# This file will make Figure S3 and S4 for the appendix comparing alternative scaling laws using state variable data from data_statevariables.txt and the biomass prediction code biomass.py
# In[1]:
import numpy as np
import pandas as pd
import biomass as bm
from scipy.stats import linregress
import matplotlib.pyplot as plt
# In[2]:
# Import the data
data = pd.read_csv('data_statevariables.csv')
# In[3]:
# Now add a column for predicted numerical biomass data
data['pBnum_2_3'] = np.zeros(len(data))
# Iterate through each row and append the biomass information
for index, row in data.iterrows():
# Get 2/3
dtemp = {'S': row['S'], 'N': row['N'], 'E': row['E_2_3']}
data.loc[index,'pBnum_2_3'] = bm.biomass(dtemp,power=3/2)
# # General plot setup
# In[5]:
# Choose color scheme
cm = 'winter'#'viridis'
# Get max and min species richness for colour scheme
smin = np.min(data['S'])
smax = np.max(data['S'])
# Set up normalization
norm = plt.Normalize(np.log(smin),np.log(smax))
# Get list of site types
stype = data['Type'].unique()
# Make marker list. Has to be same length as stype
mlist = ['s','^','D','o','X']
# # Alternative scaling
# In[7]:
# Figure S2
fig,ax = plt.subplots(figsize=(4,4))
# Plot data
xdata = np.log(data['pBnum_2_3'])
ydata = np.log(data['B_2_3'])
# Loop through each site type to put a different marker
for m,s in zip(mlist,stype):
inds = data['Type']==s
im = ax.scatter(xdata[inds],ydata[inds],marker=m,c=np.log(data['S'][inds]),cmap=cm,norm=norm,edgecolor='0.3')
# Colorbar
cax = fig.add_axes([0.94, 0.28, 0.05, 0.43])
fig.colorbar(im, cax = cax,label='ln(S)')
# Set range
ymin = np.floor(np.min(ydata))
ymax = np.ceil(np.max(ydata))
xmin = np.floor(np.min(xdata))
xmax = np.ceil(np.max(xdata))
# Set range min as min of those
rmin = np.min([ymin,xmin])
rmax = np.max([ymax,xmax])
ax.set_ylim(rmin,rmax)
ax.set_xlim(rmin,rmax)
# Labels
ax.set_xlabel('ln(Predicted B)')
ax.set_ylabel('ln(Observed B)')
# Add in R^2 value from regression
lin = linregress(xdata,ydata)
xlin = np.linspace(xmin,xmax)
ax.annotate(r'$R^2 = {:.3f}$'.format(lin[2]**2),(0.64,0.17),xycoords='figure fraction')
# Note: Without colorbar, location is 0.73, 0.17.
# Legend
# Plot a bunch of empty points. Not sure if this is the best way, but it's how I'm doing it!
leg = {}
for m,s in zip(mlist,stype):
leg[s], = ax.plot([],[],c='0.3',marker=m,linestyle="None")
# Plot 1:1 line at the back and add to legend codes
xrange = np.linspace(rmin,rmax)
leg['1:1'], = ax.plot(xrange,xrange,lw=2,c='0.3',zorder=0)#,label='1:1 line') # Can add this back in
lcodes = np.insert(stype,0,'1:1')
ax.legend([leg[s] for s in lcodes],lcodes,prop={"size":7.3})#,frameon=False)#borderpad=0.0)
# Save
fig.savefig('Figures/figS3.pdf',bbox_inches='tight')
# In[8]:
# Figure S3
fig,ax = plt.subplots(figsize=(4,4))
# Plot data
xdata = data['E_2_3']/data['pBnum_2_3']**(2/3)
ydata = data['E_2_3']/data['B_2_3']**(2/3)
# Loop through each site type to put a different marker
for m,s in zip(mlist,stype):
inds = data['Type']==s
im = ax.scatter(xdata[inds],ydata[inds],marker=m,c=np.log(data['S'][inds]),cmap=cm,norm=norm,edgecolor='0.3')
# Colorbar
cax = fig.add_axes([0.94, 0.28, 0.05, 0.43])
fig.colorbar(im, cax = cax,label='ln(S)')
# Set range
ymin = np.floor(np.min(ydata))
ymax = np.ceil(np.max(ydata))
xmin = np.floor(np.min(xdata))
xmax = np.ceil(np.max(xdata))
# Set range min as min of those
rmin = np.min([ymin,xmin])
rmax = np.max([ymax,xmax])
ax.set_ylim(rmin,rmax)
ax.set_xlim(rmin,rmax)
# Labels
ax.set_xlabel(r'Predicted ratio $E:B^{2/3}$')
ax.set_ylabel(r'Observed ratio $E:B^{2/3}$')
# Add in R^2 value from regression
lin = linregress(xdata,ydata)
xlin = np.linspace(xmin,xmax)
ax.annotate(r'$R^2 = {:.3f}$'.format(lin[2]**2),(0.64,0.42),xycoords='figure fraction')
# Previous location wo colorbar was (0.18,0.82)
# Legend
# Plot a bunch of empty points. Not sure if this is the best way, but it's how I'm doing it!
leg = {}
for m,s in zip(mlist,stype):
leg[s], = ax.plot([],[],c='0.3',marker=m,linestyle="None")
# Plot 1:1 line at the back and add to legend codes
xrange = np.linspace(rmin,rmax)
leg['1:1'], = ax.plot(xrange,xrange,lw=2,c='0.3',zorder=0)#,label='1:1 line') # Can add this back in
lcodes = np.insert(stype,0,'1:1')
ax.legend([leg[s] for s in lcodes],lcodes,prop={"size":7.3})#,frameon=False#borderpad=0.0)
# Save
fig.savefig('Figures/figS4.pdf',bbox_inches='tight')
# In[ ]: