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

[ENH] more little tweaks to the AFQ viz utils panelfigure class #1084

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
29 changes: 19 additions & 10 deletions AFQ/viz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class PanelFigure():
into subplots using matplotlib
"""

def __init__(self, num_rows, num_cols, width, height):
def __init__(self, num_rows, num_cols, width, height,
panel_label_kwargs={}):
"""
Initialize PanelFigure.

Expand All @@ -162,14 +163,27 @@ def __init__(self, num_rows, num_cols, width, height):
Width of figure in inches
height : int
Height of figure in inches
panel_label_kwargs : dict
Additional arguments for matplotlib's text method,
which is used to add panel labels to each subplot
"""
self.fig = plt.figure(figsize=(width, height))
self.grid = plt.GridSpec(num_rows, num_cols, hspace=0, wspace=0)
self.subplot_count = 0
self.panel_label_kwargs = dict(
fontfamily="Helvetica-Bold",
fontsize="xx-large",
color="white",
fontweight='bold',
verticalalignment="top",
bbox=dict(
facecolor='none',
edgecolor='none'))
self.panel_label_kwargs.update(panel_label_kwargs)

def add_img(self, fname, x_coord, y_coord, reduct_count=1,
subplot_label_pos=(0.1, 1.0), legend=None, legend_kwargs={},
add_panel_label=True, panel_label_font_size="medium"):
add_panel_label=True):
"""
Add image from fname into figure as a panel.

Expand All @@ -196,9 +210,6 @@ def add_img(self, fname, x_coord, y_coord, reduct_count=1,
add_panel_label : bool
Whether or not to add a panel label to the subplot
Default: True
panel_label_font_size : str
Font size of panel label
Default: "medium"
"""
ax = self.fig.add_subplot(self.grid[y_coord, x_coord])
im1 = Image.open(fname)
Expand All @@ -216,11 +227,9 @@ def add_img(self, fname, x_coord, y_coord, reduct_count=1,
10 / 72, -5 / 72, self.fig.dpi_scale_trans)
ax.text(
subplot_label_pos[0], subplot_label_pos[1],
f"{chr(65+self.subplot_count)})",
f"{chr(65+self.subplot_count)}",
transform=ax.transAxes + trans,
fontsize=panel_label_font_size, verticalalignment="top",
fontfamily='serif',
bbox=dict(facecolor='0.7', edgecolor='none', pad=3.0))
**self.panel_label_kwargs)
ax.imshow(np.asarray(im1), aspect=1)
ax.axis('off')
self.subplot_count = self.subplot_count + 1
Expand All @@ -242,7 +251,7 @@ def format_and_save_figure(self, fname, trim_final=True):
if trim_final:
im1 = Image.open(fname)
im1 = trim(im1)
im1.save(fname)
im1.save(fname, dpi=(300, 300))


def get_eye(view, direc):
Expand Down
16 changes: 16 additions & 0 deletions examples/tutorial_examples/plot_003_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from fury.colormap import create_colormap

import AFQ.data.fetch as afd
from AFQ.viz.utils import PanelFigure

##############################################################################
# Get some data from HBN POD2
Expand Down Expand Up @@ -418,6 +419,21 @@ def slice_volume(data, x=None, y=None, z=None):

window.record(scene, out_path='arc_cst4.png', size=(2400, 2400))

#############################################################################
# Making a Figure out of many fury panels
# ---------------------------------------
# We can also make a figure that contains multiple panels, each of which
# contains a different visualization. This is useful for communicating the
# results of an analysis. Here, we will make a figure with four panels, using
# some of the visualizations we have already created. We will use some
# convenient methods from pyAFQ.

pf = PanelFigure(3, 2, 6, 9)
pf.add_img(f'arc_cst1.png', 0, 0)
pf.add_img(f'arc_cst2.png', 1, 0)
pf.add_img(f'arc_cst3.png', 0, 1)
pf.add_img(f'arc_cst4.png', 1, 1)
pf.format_and_save_figure(f"arc_cst_fig.png")

#############################################################################
#
Expand Down
Loading