Skip to content

Commit 55386eb

Browse files
authored
Merge pull request #80 from liuzhenqi77/update-plot-mod-heatmap
[ENH] add features to plot_mod_heatmap
2 parents e16634d + 87b95df commit 55386eb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

netneurotools/plotting.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def sort_communities(consensus, communities):
7575
def plot_mod_heatmap(data, communities, *, inds=None, edgecolor='black',
7676
ax=None, figsize=(6.4, 4.8), xlabels=None, ylabels=None,
7777
xlabelrotation=90, ylabelrotation=0, cbar=True,
78+
square=True, xticklabels=None, yticklabels=None,
7879
mask_diagonal=True, **kwargs):
7980
"""
8081
Plots `data` as heatmap with borders drawn around `communities`
@@ -102,6 +103,11 @@ def plot_mod_heatmap(data, communities, *, inds=None, edgecolor='black',
102103
{x,y}labelrotation : float, optional
103104
Angle of the rotation of the labels. Available only if `{x,y}labels`
104105
provided. Default : xlabelrotation: 90, ylabelrotation: 0
106+
square : bool, optional
107+
Setting the matrix with equal aspect. Default: True
108+
{x,y}ticklabels : list, optional
109+
Incompatible with `{x,y}labels`. List of labels for each entry (not
110+
community) in `data`. Default: None
105111
cbar : bool, optional
106112
Whether to plot colorbar. Default: True
107113
mask_diagonal : bool, optional
@@ -115,6 +121,10 @@ def plot_mod_heatmap(data, communities, *, inds=None, edgecolor='black',
115121
Axis object containing plot
116122
"""
117123

124+
for t, l in zip([xticklabels, yticklabels], [xlabels, ylabels]):
125+
if t is not None and l is not None:
126+
raise ValueError('Cannot set both {x,y}labels and {x,y}ticklabels')
127+
118128
# get indices for sorting consensus
119129
if inds is None:
120130
inds = sort_communities(data, communities)
@@ -132,6 +142,10 @@ def plot_mod_heatmap(data, communities, *, inds=None, edgecolor='black',
132142
coll = ax.pcolormesh(plot_data, edgecolor='none', **kwargs)
133143
ax.set(xlim=(0, plot_data.shape[1]), ylim=(0, plot_data.shape[0]))
134144

145+
# set equal aspect
146+
if square:
147+
ax.set_aspect('equal')
148+
135149
for side in ['top', 'right', 'left', 'bottom']:
136150
ax.spines[side].set_visible(False)
137151

@@ -179,6 +193,15 @@ def plot_mod_heatmap(data, communities, *, inds=None, edgecolor='black',
179193
ax.set_yticklabels(labels=ylabels, rotation=ylabelrotation)
180194
ax.tick_params(left=False, bottom=False)
181195

196+
if xticklabels is not None:
197+
labels_ind = [xticklabels[i] for i in inds]
198+
ax.set_xticks(np.arange(len(labels_ind)) + 0.5)
199+
ax.set_xticklabels(labels_ind, rotation=90)
200+
if yticklabels is not None:
201+
labels_ind = [yticklabels[i] for i in inds]
202+
ax.set_yticks(np.arange(len(labels_ind)) + 0.5)
203+
ax.set_yticklabels(labels_ind)
204+
182205
return ax
183206

184207

0 commit comments

Comments
 (0)