Skip to content

Commit 43954bd

Browse files
committed
update diet matrix
1 parent 89739f1 commit 43954bd

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

apecosm/mplot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
PROJIN = ccrs.PlateCarree()
2222

23-
def plot_diet_values(data, const, community_index, legend_args={}, **kwargs):
23+
def plot_diet_values(diet_data, const, community_index, draw_legend=False, legend_args={}, **kwargs):
2424

2525
community_names = extract_community_names(const)
2626
n_community = len(community_names)
@@ -31,14 +31,15 @@ def plot_diet_values(data, const, community_index, legend_args={}, **kwargs):
3131

3232
ax = plt.gca()
3333
length = const['length'].isel(c=community_index)
34-
diet = data['community_diet_values'].isel(c=community_index)#.compute()
34+
diet = diet_data.isel(c=community_index)#.compute()
3535
repf = diet.sum(dim='prey_group')
3636
l = ax.stackplot(length, diet.T, edgecolor='k', **kwargs)
3737
ax.set_xscale('log')
3838
ax.set_xlim(length.min(), length.max())
3939
ax.set_ylim(0, repf.max())
4040
ax.set_title(community_names[community_index])
41-
ax.legend(legend, **legend_args)
41+
if draw_legend:
42+
ax.legend(legend, **legend_args)
4243
return l
4344

4445
def plot_oope_map(data, mesh, axis=None, draw_land=True, **kwargs):

doc/computations/diet.rst

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import apecosm
88
import xarray as xr
99
import matplotlib.pyplot as plt
10+
import matplotlib
1011
1112
domain_ds = xr.open_dataset(os.path.join('doc', 'data', 'domains.nc'))
1213
domain = domain_ds['domain_1']
@@ -18,11 +19,119 @@
1819
const = apecosm.open_constants(os.path.join('doc', 'data', 'apecosm/'))
1920
const
2021
21-
data = apecosm.open_apecosm_data(os.path.join('doc', 'data', 'apecosm'))
22+
data = apecosm.open_apecosm_data(os.path.join('doc', 'data', 'apecosm'),
23+
replace_dims={'community': 'c'})
2224
data
2325
2426
**********************
2527
Diet calculation
2628
**********************
2729

28-
If the Apecosm simulation has been
30+
If the diet outputs are available in the Apecosm
31+
simulations, it is possible to plot the diet matrix. This is
32+
achieved as follows.
33+
34+
First, the spatial mean diet is computed using the
35+
:py:func:`apecosm.extract_weighted_data` function.
36+
37+
.. ipython:: python
38+
39+
mean_diet = apecosm.extract_weighted_data(data, const, mesh, 'community_diet_values')
40+
mean_diet
41+
42+
.. ipython:: python
43+
:suppress:
44+
45+
mean_diet = mean_diet.compute()
46+
47+
Then, the time average is computed using the
48+
:py:func:`apecosm.extract_time_means` function:
49+
50+
.. ipython:: python
51+
52+
time_average_mean_diet = apecosm.extract_time_means(mean_diet)
53+
time_average_mean_diet
54+
55+
.. ipython:: python
56+
:suppress:
57+
58+
time_average_mean_diet = time_average_mean_diet.compute()
59+
60+
Now, the drawing of the diet matrix is done by using the
61+
:py:func:`plot_diet_values` function:
62+
63+
.. ipython:: python
64+
65+
fig = plt.figure()
66+
ax = plt.gca()
67+
l = apecosm.plot_diet_values(time_average_mean_diet, const, 0)
68+
plt.savefig(os.path.join('doc', 'computations', '_static', 'diet_com_0.jpg'), bbox_inches='tight')
69+
plt.savefig(os.path.join('doc', 'computations', '_static', 'diet_com_0.pdf'), bbox_inches='tight')
70+
plt.close(fig)
71+
72+
.. figure:: _static/diet_com_0.*
73+
:align: center
74+
75+
Mean diet for community 0
76+
77+
78+
The first argument is the spatially and time averaged diet matrix,
79+
the second argument is the dataset containing all the Apecosm constants
80+
and the third argument is the community index.
81+
82+
Note that there is the possibility to control the legend layout by
83+
providing a `legend_args` argument, which is a dictionary containing
84+
the legend arguments.
85+
86+
Furthermore, the arguments of the Matplotlib ``stackplot`` function
87+
can also be included in the :py:func:`apecosm.plot_diet_values` function.
88+
89+
For instance:
90+
91+
.. ipython:: python
92+
93+
cmap = matplotlib.colormaps['jet']
94+
colors = [cmap(i / 10) for i in range(11)]
95+
96+
fig = plt.figure()
97+
ax = plt.gca()
98+
l = apecosm.plot_diet_values(time_average_mean_diet, const, 0,
99+
colors=colors, alpha=0.5,
100+
legend_args={'ncol': 2, 'fontsize': 6})
101+
102+
plt.savefig(os.path.join('doc', 'computations', '_static', 'upt_diet_com_0.jpg'), bbox_inches='tight')
103+
plt.savefig(os.path.join('doc', 'computations', '_static', 'upt_diet_com_0.pdf'), bbox_inches='tight')
104+
plt.close(fig)
105+
106+
.. figure:: _static/upt_diet_com_0.*
107+
:align: center
108+
109+
Mean diet for community 0 with additional arguments for
110+
controling the legend and stackplot display
111+
112+
To draw the diet matrix for all communities, a loop
113+
must be done over the `c` dimension:
114+
115+
.. ipython:: python
116+
117+
fig = plt.figure(figsize=(12, 8))
118+
plt.subplots_adjust(hspace=0.5)
119+
120+
for c in range(5):
121+
ax = plt.subplot(3, 2, c + 1)
122+
draw_legend = (c == 0)
123+
l = apecosm.plot_diet_values(time_average_mean_diet, const, c,
124+
colors=colors, alpha=0.5, draw_legend=draw_legend,
125+
legend_args={'ncol': 2, 'fontsize': 6, 'framealpha': 1})
126+
ax.set_xlabel('Length (m)')
127+
ax.set_xlim(const['length'].min(), const['length'].max())
128+
129+
plt.savefig(os.path.join('doc', 'computations', '_static', 'full_diet.jpg'), bbox_inches='tight')
130+
plt.savefig(os.path.join('doc', 'computations', '_static', 'full_diet.pdf'), bbox_inches='tight')
131+
plt.close(fig)
132+
133+
.. figure:: _static/full_diet.*
134+
:align: center
135+
136+
Mean diet for all communities with additional arguments for
137+
controling the legend and stackplot display

0 commit comments

Comments
 (0)