diff --git a/brainglobe_template_builder/plots.py b/brainglobe_template_builder/plots.py index 2bf4bc6..76799c4 100644 --- a/brainglobe_template_builder/plots.py +++ b/brainglobe_template_builder/plots.py @@ -215,6 +215,8 @@ def plot_orthographic( show_slices: list[int] | None = None, pad_sizes: list[tuple[int, int]] | None = None, mip_attenuation: float = 0.01, + scale_bar: bool = False, + resolution: float = 0.025, save_path: Path | None = None, ) -> tuple[plt.Figure, np.ndarray]: """Plot orthographic views of a 3D image, @@ -258,6 +260,29 @@ def plot_orthographic( ax.axhline(slice_idxs[h], color="r", linestyle="--", alpha=0.5) ax.axvline(slice_idxs[v], color="r", linestyle="--", alpha=0.5) + # Add scale bar equal to 1mm to each view + if scale_bar: + bar_length = 1 / resolution + bar_x = frames[j].shape[1] - bar_length - 20 + bar_y = frames[j].shape[0] - 10 + ax.plot( + [bar_x, bar_x + bar_length], + [bar_y, bar_y], + color="w", + linewidth=2, + clip_on=False, + ) + # Add the text on if it's the top view + if j == 3: + ax.text( + bar_x + bar_length / 2, + bar_y - 5, + "1 mm", + color="w", + ha="center", + va="bottom", + ) + ax = _clear_spines_and_ticks(ax) fig.subplots_adjust( @@ -275,6 +300,8 @@ def plot_inset_comparison( y_min: int, x_min: int, size: int, + scale_bar: bool = False, + resolution: float = 0.025, save_path: Path | None = None, ): """Plot the same inset from two images side by side. @@ -325,6 +352,27 @@ def plot_inset_comparison( ax.axis("off") ax.set_title(name) + if scale_bar: + bar_length = 1 / resolution + bar_x = img_inset.shape[1] - bar_length - 5 + bar_y = img_inset.shape[0] - 5 + ax.plot( + [bar_x, bar_x + bar_length], + [bar_y, bar_y], + color="w", + linewidth=2, + clip_on=False, + ) + if i == 1: # Add the text on the second image only + ax.text( + bar_x + bar_length / 2, + bar_y - 2.5, + "1 mm", + color="w", + ha="center", + va="bottom", + ) + fig.tight_layout() if save_path: save_figure(fig, save_path.parent, save_path.name.split(".")[0]) diff --git a/examples/plots/template_and_individual.py b/examples/plots/template_and_individual.py index 1ea332a..b0a505f 100644 --- a/examples/plots/template_and_individual.py +++ b/examples/plots/template_and_individual.py @@ -25,7 +25,7 @@ # Load matplotlib parameters (to allow for proper font export) plt.style.use(current_dir / "plots.mplstyle") # Load config file containing template building parameters -config = load_config(current_dir / "config_25um.yaml") +config = load_config(current_dir / "config_50um.yaml") # Setup directories based on config file atlas_dir, template_dir, plots_dir = setup_directories(config) @@ -101,6 +101,8 @@ show_slices=config["show_slices"], pad_sizes=pad_sizes, mip_attenuation=config["mip_attenuation"], + scale_bar=True, + resolution=config["resolution_um"] * 1e-3, # convert to mm save_path=plots_dir / "final_template_orthographic", ) print("Plotted final template in orthographic view") @@ -116,6 +118,8 @@ show_slices=config["show_slices"], pad_sizes=pad_sizes, mip_attenuation=config["mip_attenuation"], + scale_bar=True, + resolution=config["resolution_um"] * 1e-3, # convert to mm save_path=plots_dir / f"{example_subject}_orthographic", ) print(f"Plotted {example_subject} in orthographic view") @@ -129,5 +133,7 @@ img2=("template", template_img), **inset_param, save_path=plots_dir / plot_file_name, + scale_bar=True, + resolution=config["resolution_um"] * 1e-3, # convert to mm ) print(f"Plotted inset comparison between {example_subject} and template")