|
10 | 10 |
|
11 | 11 |
|
12 | 12 | def plot_gLV(yobs, timepoints): |
13 | | - # fig, axs = plt.subplots(1, 2, layout='constrained') |
| 13 | + # fig, axs = plt.subplots(1, 2, layout='constrained') # Optional |
| 14 | + # alternative |
14 | 15 | fig, axs = plt.subplots(1, 1) |
15 | 16 | for species_idx in range(yobs.shape[1]): |
16 | | - axs.plot(timepoints, yobs[:, species_idx], color=cols[species_idx]) |
| 17 | + label = f'Species {species_idx + 1}' # Add a label for each species |
| 18 | + axs.plot(timepoints, yobs[:, species_idx], |
| 19 | + color=cols[species_idx], label=label) |
| 20 | + |
17 | 21 | axs.set_xlabel('time') |
18 | 22 | axs.set_ylabel('[species]') |
| 23 | + axs.legend() # Ensure the legend is called on the correct axes |
| 24 | + plt.show() |
19 | 25 |
|
20 | 26 |
|
21 | 27 | def plot_CRM(observed_species, observed_resources, timepoints, csv_file=None): |
@@ -102,49 +108,80 @@ def plot_CRM(observed_species, observed_resources, timepoints, csv_file=None): |
102 | 108 |
|
103 | 109 | return fig, ax |
104 | 110 |
|
105 | | -def plot_CRM_with_intervals(observed_species, observed_resources, species_lower, species_upper, |
106 | | - resource_lower, resource_upper, times, filename=None): |
| 111 | + |
| 112 | +def plot_CRM_with_intervals( |
| 113 | + observed_species, |
| 114 | + observed_resources, |
| 115 | + species_lower, |
| 116 | + species_upper, |
| 117 | + resource_lower, |
| 118 | + resource_upper, |
| 119 | + times, |
| 120 | + filename=None): |
107 | 121 | fig, ax = plt.subplots(figsize=(12, 8)) |
108 | | - |
| 122 | + |
109 | 123 | # Plot median trajectories |
110 | 124 | for i in range(observed_species.shape[1]): |
111 | | - ax.plot(times, observed_species[:, i], label=f'Species {i+1}', linewidth=2) |
112 | | - |
| 125 | + ax.plot(times, observed_species[:, i], |
| 126 | + label=f'Species {i+1}', linewidth=2) |
| 127 | + |
113 | 128 | for i in range(observed_resources.shape[1]): |
114 | | - ax.plot(times, observed_resources[:, i], label=f'Resource {i+1}', linewidth=2, linestyle='--') |
115 | | - |
116 | | - # Add confidence ribbons |
| 129 | + ax.plot(times, |
| 130 | + observed_resources[:, |
| 131 | + i], |
| 132 | + label=f'Resource {i+1}', |
| 133 | + linewidth=2, |
| 134 | + linestyle='--') |
| 135 | + |
| 136 | + # Add confidence ribbons |
117 | 137 | for i in range(observed_species.shape[1]): |
118 | | - ax.fill_between(times, species_lower[:, i], species_upper[:, i], |
119 | | - alpha=0.2, color=plt.cm.tab10(i)) |
120 | | - |
| 138 | + ax.fill_between(times, species_lower[:, i], species_upper[:, i], |
| 139 | + alpha=0.2, color=plt.cm.tab10(i)) |
| 140 | + |
121 | 141 | for i in range(observed_resources.shape[1]): |
122 | | - ax.fill_between(times, resource_lower[:, i], resource_upper[:, i], |
123 | | - alpha=0.2, color=plt.cm.tab10(i + observed_species.shape[1])) |
124 | | - |
| 142 | + ax.fill_between(times, |
| 143 | + resource_lower[:, |
| 144 | + i], |
| 145 | + resource_upper[:, |
| 146 | + i], |
| 147 | + alpha=0.2, |
| 148 | + color=plt.cm.tab10(i + observed_species.shape[1])) |
| 149 | + |
125 | 150 | if filename: |
126 | 151 | true_data = pd.read_csv(filename) |
127 | 152 | true_times = true_data['time'].values |
128 | | - |
| 153 | + |
129 | 154 | for i in range(observed_species.shape[1]): |
130 | 155 | col_name = f'species_{i+1}' |
131 | 156 | if col_name in true_data.columns: |
132 | | - ax.scatter(true_times, true_data[col_name], |
133 | | - marker='o', s=30, color=plt.cm.tab10(i), label=f'True {col_name}') |
134 | | - |
| 157 | + ax.scatter( |
| 158 | + true_times, |
| 159 | + true_data[col_name], |
| 160 | + marker='o', |
| 161 | + s=30, |
| 162 | + color=plt.cm.tab10(i), |
| 163 | + label=f'True {col_name}') |
| 164 | + |
135 | 165 | for i in range(observed_resources.shape[1]): |
136 | 166 | col_name = f'resource_{i+1}' |
137 | 167 | if col_name in true_data.columns: |
138 | | - ax.scatter(true_times, true_data[col_name], |
139 | | - marker='s', s=30, color=plt.cm.tab10(i + observed_species.shape[1]), |
140 | | - label=f'True {col_name}') |
141 | | - |
| 168 | + ax.scatter( |
| 169 | + true_times, |
| 170 | + true_data[col_name], |
| 171 | + marker='s', |
| 172 | + s=30, |
| 173 | + color=plt.cm.tab10( |
| 174 | + i + observed_species.shape[1]), |
| 175 | + label=f'True {col_name}') |
| 176 | + |
142 | 177 | ax.set_xlabel('Time', fontsize=14) |
143 | 178 | ax.set_ylabel('Concentration', fontsize=14) |
144 | | - ax.set_title('Consumer-Resource Model Dynamics with 95% Credible Intervals', fontsize=16) |
| 179 | + ax.set_title( |
| 180 | + 'Consumer-Resource Model Dynamics with 95% Credible Intervals', |
| 181 | + fontsize=16) |
145 | 182 | ax.legend(loc='best', fontsize=12) |
146 | 183 | ax.grid(True, alpha=0.3) |
147 | | - |
| 184 | + |
148 | 185 | plt.tight_layout() |
149 | 186 | if filename: |
150 | 187 | plt.savefig(f"{filename.split('.')[0]}_with_intervals.png", dpi=300) |
|
0 commit comments