Skip to content

Commit 703f2e2

Browse files
committed
Plot shared legend
1 parent 06e8ca9 commit 703f2e2

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

scripts/figures/plot_fig4.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,94 @@ def group_lr(names_lr, values):
156156

157157
return names, values_left, values_right
158158

159+
def plot_legend(chreef_data, save_path, grouping="side_mono", use_alias=True,
160+
alignment="horizontal"):
161+
"""Plot common legend for figures 4c, 4d, and 4e.
162+
163+
Args:
164+
chreef_data: Data of ChReef cochleae.
165+
save_path: save path to save legend.
166+
grouping: Grouping for cochleae.
167+
"side_mono" for division in Injected and Non-Injected.
168+
"side_multi" for division per cochlea.
169+
"animal" for division per animal.
170+
use_alias: Use alias.
171+
"""
172+
if use_alias:
173+
alias = [COCHLEAE_DICT[k]["alias"] for k in chreef_data.keys()]
174+
else:
175+
alias = [name.replace("_", "").replace("0", "") for name in chreef_data.keys()]
176+
177+
sgns = [len(vals) for vals in chreef_data.values()]
178+
alias, values_left, values_right = group_lr(alias, sgns)
179+
180+
colors = ["crimson", "purple", "gold"]
181+
if grouping == "side_mono":
182+
colors = [COLOR_LEFT, COLOR_RIGHT]
183+
labels = ["Injected", "Non-Injected"]
184+
markers = [MARKER_LEFT, MARKER_RIGHT]
185+
ncol = 2
186+
187+
elif grouping == "side_multi":
188+
colors = []
189+
labels = []
190+
markers = []
191+
keys_left = list(COLORS_LEFT.keys())
192+
keys_right = list(COLORS_RIGHT.keys())
193+
for num in range(len(COLORS_LEFT)):
194+
colors.append(COLORS_LEFT[keys_left[num]])
195+
colors.append(COLORS_RIGHT[keys_right[num]])
196+
labels.append(f"{alias[num]}L")
197+
labels.append(f"{alias[num]}R")
198+
markers.append(MARKER_LEFT)
199+
markers.append(MARKER_RIGHT)
200+
if alignment == "vertical":
201+
colors = colors[::2] + colors[1::2]
202+
labels = labels[::2] + labels[1::2]
203+
markers = markers[::2] + markers[1::2]
204+
ncol = 2
205+
else:
206+
ncol = 5
207+
208+
elif grouping == "animal":
209+
colors = []
210+
labels = []
211+
markers = []
212+
ncol = 5
213+
keys_animal = list(COLORS_ANIMAL.keys())
214+
for num in range(len(COLORS_ANIMAL)):
215+
colors.append(COLORS_ANIMAL[keys_animal[num]])
216+
colors.append(COLORS_ANIMAL[keys_animal[num]])
217+
labels.append(f"{alias[num]}L")
218+
labels.append(f"{alias[num]}R")
219+
markers.append(MARKER_LEFT)
220+
markers.append(MARKER_RIGHT)
221+
if alignment == "vertical":
222+
colors = colors[::2] + colors[1::2]
223+
labels = labels[::2] + labels[1::2]
224+
markers = markers[::2] + markers[1::2]
225+
ncol = 2
226+
else:
227+
ncol = 5
228+
229+
else:
230+
raise ValueError("Choose a correct 'grouping' parameter.")
231+
232+
f = lambda m,c: plt.plot([],[], marker=m, color=c, ls="none")[0]
233+
handles = [f(marker, color) for (color, marker) in zip(colors, markers)]
234+
legend = plt.legend(handles, labels, loc=3, ncol=ncol, framealpha=1, frameon=False)
235+
236+
def export_legend(legend, filename="fig_04_legend.png"):
237+
legend.axes.axis("off")
238+
fig = legend.figure
239+
fig.canvas.draw()
240+
bbox = legend.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
241+
fig.savefig(filename, bbox_inches=bbox, dpi=png_dpi)
242+
243+
export_legend(legend, save_path)
244+
legend.remove()
245+
plt.close()
246+
159247

160248
def fig_04c(chreef_data, save_path, plot=False, grouping="side_mono", use_alias=True):
161249
"""Box plot showing the SGN counts of ChReef treated cochleae compared to healthy ones.
@@ -677,6 +765,13 @@ def main():
677765
# remove other cochlea to have only pairs remaining
678766
chreef_data.pop("M_LR_000143_R")
679767

768+
plot_legend(chreef_data, grouping="side_mono",
769+
save_path=os.path.join(args.figure_dir, f"fig_04_legend_mono.{FILE_EXTENSION}"))
770+
plot_legend(chreef_data, grouping="side_multi",
771+
save_path=os.path.join(args.figure_dir, f"fig_04_legend_multi.{FILE_EXTENSION}"))
772+
plot_legend(chreef_data, grouping="animal",
773+
save_path=os.path.join(args.figure_dir, f"fig_04_legend_animal.{FILE_EXTENSION}"))
774+
680775
# Create the panels:
681776
grouping = "side_multi"
682777

0 commit comments

Comments
 (0)