Skip to content

Commit 013ccb6

Browse files
author
Jannis Teunissen
committed
Small plot improvements in chemistry_visualize_rates.py
1 parent 82e6311 commit 013ccb6

1 file changed

Lines changed: 49 additions & 42 deletions

File tree

tools/chemistry_visualize_rates.py

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,58 @@
6666

6767
if args.plot_all:
6868
# Visualize all the rates vs. time
69-
plt.figure(figsize=(8, 8))
69+
fig, ax = plt.subplots(figsize=(10, 6), layout='constrained')
7070

7171
ix = np.argsort(rates[-1, :])[::-1]
7272
sum_of_rates = rates[-1, :].sum()
7373

7474
for i in ix:
75-
plt.plot(time, rates[:, i], label=reactions_list[i] +
76-
f' ({100*rates[-1, i]/sum_of_rates:.2f}%)')
77-
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
78-
79-
# Visualize the source and sink reactions for a given specie
80-
sidx = species_list.index(args.soi)
81-
fig, ax = plt.subplots(3, figsize=(5, 7.5), sharex=True,
82-
layout='constrained')
83-
84-
srce_idx = np.where(stoich_matrix[sidx, :] > 0)[0]
85-
sink_idx = np.where(stoich_matrix[sidx, :] < 0)[0]
86-
titles = ['Source', 'Sink']
87-
88-
for i, (ix, text) in enumerate(zip([srce_idx, sink_idx], titles)):
89-
amount = stoich_matrix[sidx, ix] * rates[:, ix]
90-
frac = amount[-1]/amount[-1].sum()
91-
92-
for j, idx in enumerate(ix):
93-
if frac[j] > args.threshold:
94-
ax[i].plot(time, amount[:, j], label=reactions_list[idx] +
95-
f' ({100*frac[j]:.2f}%)')
96-
97-
ax[i].set_title(text + ' reactions')
98-
ax[i].set_ylabel('Production (#)')
99-
ax[i].legend()
100-
101-
gross_prod = np.dot(rates[:, srce_idx], stoich_matrix[sidx, srce_idx])
102-
net_prod = np.dot(rates, stoich_matrix[sidx])
103-
ax[2].plot(time, gross_prod, label='gross production')
104-
ax[2].plot(time, net_prod, label='net production')
105-
ax[2].plot(time, amounts[:, sidx], '--', label='amount present')
106-
ax[2].set_xlabel('Time (s)')
107-
ax[2].set_ylabel('Production (#)')
108-
ax[2].legend()
109-
fig.suptitle(f'{len(srce_idx)+len(sink_idx)} of {n_reactions}' +
110-
f' influence {args.soi}')
111-
112-
if args.savefig is not None:
113-
plt.savefig(args.savefig, bbox_inches='tight', dpi=200)
114-
print(f'Saved {args.savefig}')
115-
else:
75+
ax.plot(time, rates[:, i], label=reactions_list[i] +
76+
f' ({100*rates[-1, i]/sum_of_rates:.2f}%)')
77+
ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
78+
ax.set_ylabel('Production (#)')
79+
ax.set_xlabel('Time (s)')
11680
plt.show()
81+
82+
if args.soi is not None:
83+
# Visualize the source and sink reactions for a given specie
84+
sidx = species_list.index(args.soi)
85+
fig, ax = plt.subplots(3, figsize=(5, 7.5), sharex=True,
86+
layout='constrained')
87+
88+
srce_idx = np.where(stoich_matrix[sidx, :] > 0)[0]
89+
sink_idx = np.where(stoich_matrix[sidx, :] < 0)[0]
90+
titles = ['Source', 'Sink']
91+
92+
for i, (ix, text) in enumerate(zip([srce_idx, sink_idx], titles)):
93+
amount = stoich_matrix[sidx, ix] * rates[:, ix]
94+
frac = amount[-1]/amount[-1].sum()
95+
n_shown = 0
96+
97+
for j, idx in enumerate(ix):
98+
if frac[j] > args.threshold:
99+
ax[i].plot(time, amount[:, j], label=reactions_list[idx] +
100+
f' ({100*frac[j]:.2f}%)')
101+
n_shown += 1
102+
103+
ax[i].set_title(text + ' reactions')
104+
ax[i].set_ylabel('Production (#)')
105+
if n_shown > 0:
106+
ax[i].legend()
107+
108+
gross_prod = np.dot(rates[:, srce_idx], stoich_matrix[sidx, srce_idx])
109+
net_prod = np.dot(rates, stoich_matrix[sidx])
110+
ax[2].plot(time, gross_prod, label='gross production')
111+
ax[2].plot(time, net_prod, label='net production')
112+
ax[2].plot(time, amounts[:, sidx], '--', label='amount present')
113+
ax[2].set_xlabel('Time (s)')
114+
ax[2].set_ylabel('Production (#)')
115+
ax[2].legend()
116+
fig.suptitle(f'{len(srce_idx)+len(sink_idx)} of {n_reactions}' +
117+
f' influence {args.soi}')
118+
119+
if args.savefig is not None:
120+
plt.savefig(args.savefig, bbox_inches='tight', dpi=200)
121+
print(f'Saved {args.savefig}')
122+
else:
123+
plt.show()

0 commit comments

Comments
 (0)