Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ax arg to plot method #44

Merged
merged 18 commits into from
Jul 24, 2024
Merged
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 138 additions & 31 deletions brainglobe_heatmap/heatmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,129 @@
def plot(
self,
show_legend: bool = False,
xlabel: str = "μm",
ylabel: str = "μm",
xlabel: str = "µm",
ylabel: str = "µm",
hide_axes: bool = False,
filename: Optional[str] = None,
cbar_label: Optional[str] = None,
show_cbar: bool = True,
**kwargs,
) -> plt.Figure:
"""
Plots the heatmap in 2D using matplotlib
Plots the heatmap in 2D using matplotlib.

This method generates a 2D visualization of the heatmap data in
a standalone matplotlib figure.

Parameters
----------
show_legend : bool, optional
If True, displays a legend for the plotted regions.
Default is False.
xlabel : str, optional
Label for the x-axis. Default is "µm".
ylabel : str, optional
Label for the y-axis. Default is "µm".
hide_axes : bool, optional
If True, hides the axes for a cleaner look. Default is False.
filename : Optional[str], optional
Path to save the figure to. If None, the figure is not saved.
Default is None.
cbar_label : Optional[str], optional
Label for the colorbar. If None, no label is displayed.
Default is None.
show_cbar : bool, optional
If True, displays a colorbar alongside the subplot.
Default is True.
**kwargs : dict
Additional keyword arguments passed to the plotting function.

Returns
-------
plt.Figure
The matplotlib figure object for the plot.

Notes
-----
This method is used to generate a standalone plot of
the heatmap data.
"""

f, ax = plt.subplots(figsize=(9, 9))

f, ax = self.plot_subplot(
fig=f,
ax=ax,
show_legend=show_legend,
xlabel=xlabel,
ylabel=ylabel,
hide_axes=hide_axes,
cbar_label=cbar_label,
show_cbar=show_cbar,
**kwargs,

Check warning on line 235 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L235

Added line #L235 was not covered by tests
)

Check warning on line 237 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L237

Added line #L237 was not covered by tests
if filename is not None:
plt.savefig(filename, dpi=300)

plt.show()
return f

def plot_subplot(
self,
fig: plt.Figure,
ax: plt.Axes,
show_legend: bool = False,
xlabel: str = "µm",
ylabel: str = "µm",

Check warning on line 250 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L249-L250

Added lines #L249 - L250 were not covered by tests
hide_axes: bool = False,
cbar_label: Optional[str] = None,
show_cbar: bool = True,

Check warning on line 253 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L252-L253

Added lines #L252 - L253 were not covered by tests
**kwargs,
) -> Tuple[plt.Figure, plt.Axes]:

Check warning on line 255 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L255

Added line #L255 was not covered by tests
"""
Plots a heatmap in a subplot within a given figure and axes.

This method is responsible for plotting a single subplot within a
larger figure, allowing for the creation of complex multi-plot
visualizations.

Parameters
----------
fig : plt.Figure, optional
The figure object in which the subplot is plotted.
ax : plt.Axes, optional
The axes object in which the subplot is plotted.
show_legend : bool, optional
If True, displays a legend for the plotted regions.
Default is False.
xlabel : str, optional
Label for the x-axis. Default is "µm".
ylabel : str, optional
Label for the y-axis. Default is "µm".
hide_axes : bool, optional
If True, hides the axes for a cleaner look. Default is False.
cbar_label : Optional[str], optional
Label for the colorbar. If None, no label is displayed.
Default is None.
show_cbar : bool, optional
Display a colorbar alongside the subplot. Default is True.
**kwargs : dict
Additional keyword arguments passed to the plotting function.

Returns
-------
plt.Figure, plt.Axes
A tuple containing the figure and axes objects used for the plot.

Notes
-----
This method modifies the provided figure and axes objects in-place.
"""
projected, _ = self.slicer.get_structures_slice_coords(
self.regions_meshes, self.scene.root
)

f, ax = plt.subplots(figsize=(9, 9))
for r, coords in projected.items():
name, segment = r.split("_segment_")
ax.fill(
Expand All @@ -201,30 +309,33 @@
alpha=0.3 if name == "root" else None,
)

# make colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

# cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=self.vmin, vmax=self.vmax)
if self.label_regions is True:
cbar = f.colorbar(
mpl.cm.ScalarMappable(
norm=None,
cmap=mpl.cm.get_cmap(self.cmap, len(self.values)),
),
cax=cax,
)
else:
cbar = f.colorbar(
mpl.cm.ScalarMappable(norm=norm, cmap=self.cmap), cax=cax
)
if show_cbar:

Check warning on line 312 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L312

Added line #L312 was not covered by tests
# make colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

# cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=self.vmin, vmax=self.vmax)
if self.label_regions is True:
cbar = fig.colorbar(
mpl.cm.ScalarMappable(
norm=None,
cmap=mpl.cm.get_cmap(self.cmap, len(self.values)),

Check warning on line 323 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L323

Added line #L323 was not covered by tests
),
cax=cax,
)

Check warning on line 326 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L325-L326

Added lines #L325 - L326 were not covered by tests
else:
cbar = fig.colorbar(
mpl.cm.ScalarMappable(norm=norm, cmap=self.cmap), cax=cax
)

Check warning on line 330 in brainglobe_heatmap/heatmaps.py

View check run for this annotation

Codecov / codecov/patch

brainglobe_heatmap/heatmaps.py#L329-L330

Added lines #L329 - L330 were not covered by tests

if cbar_label is not None:
cbar.set_label(cbar_label)
if cbar_label is not None:
cbar.set_label(cbar_label)

if self.label_regions is True:
cbar.ax.set_yticklabels([r.strip() for r in self.values.keys()])
if self.label_regions is True:
cbar.ax.set_yticklabels(
[r.strip() for r in self.values.keys()]
)

# style axes
ax.invert_yaxis()
Expand All @@ -245,11 +356,7 @@
ax.set_yticks([])
ax.set(xlabel="", ylabel="")

if filename is not None:
plt.savefig(filename, dpi=300)

if show_legend:
ax.legend()
plt.show()

return f
return fig, ax
Loading