|
7 | 7 | import apecosm
|
8 | 8 | import xarray as xr
|
9 | 9 | import matplotlib.pyplot as plt
|
| 10 | + import matplotlib |
10 | 11 |
|
11 | 12 | domain_ds = xr.open_dataset(os.path.join('doc', 'data', 'domains.nc'))
|
12 | 13 | domain = domain_ds['domain_1']
|
|
18 | 19 | const = apecosm.open_constants(os.path.join('doc', 'data', 'apecosm/'))
|
19 | 20 | const
|
20 | 21 |
|
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'}) |
22 | 24 | data
|
23 | 25 |
|
24 | 26 | **********************
|
25 | 27 | Diet calculation
|
26 | 28 | **********************
|
27 | 29 |
|
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