Subplotting plot_connectivity_circle

  • MNE version: 1.0
  • MNE-connectivity version: 0.3
  • operating system: macOS Monterey M1

I’d like to plot multiple connectivity circles on a single figure. Each circle is produced via plot_connectivity_circle. The docs imply this is possible, but I’ve not been able to get multiple attempts to work.

My first attempt seeks to make use of the ax kwarg listed in the plot_connectivity_circle docs. But I get the following error:

TypeError                                 Traceback (most recent call last)
Input In [66], in <cell line: 50>()
     31         # TODO: use different color scheme to not mislead with less connectivity
     32         plot_connectivity_circle(
     33             conn_diff,
     34             node_names=ch_names,
   (...)
     48             n_lines=n_sig,
     49         )
---> 50 plot_conn_diff_circles('J2', seg_connectivity['J2'], 'J1', seg_connectivity['J1'], perm_thresholds)

Input In [66], in plot_conn_diff_circles(name1, conn1, name2, conn2, perm_thresholds)
     29         ax = axs[row][col]
     31         # TODO: use different color scheme to not mislead with less connectivity
---> 32         plot_connectivity_circle(
     33             conn_diff,
     34             node_names=ch_names,
     35             facecolor='grey',
     36             textcolor='black',
     37             linewidth=1, 
     38             colormap='bwr',
     39             fontsize_names=8,
     40             padding=2,
     41             fontsize_colorbar=8,
     42             title=f'{band_name} {conn_method}, {name1} - {name2}',
     43 #             fig=fig,
     44             # ax is in the docs but throws a KeyError -- I'm on version 0.3 and it does not yet accept ax kwarg
     45             ax=ax,
     46 #             subplot=(row+1,col+1,1),
     47             node_angles=node_angles,
     48             n_lines=n_sig,
     49         )

TypeError: plot_connectivity_circle() got an unexpected keyword argument 'ax'

Despite being on MNE-connectivity version 0.3, which I believe is the most recent stable release:

So I tried to make use of the kwargs fig and subplot, which the docs say are soon to be deprecated, but should at least still work. This code runs, and appears to put colorbars throughout the figure, but only ever overwrites the first subplot:

Code here:

from mne_connectivity.viz import plot_connectivity_circle
from mne_connectivity import Connectivity

def plot_conn_diff_circles(name1: str, conn1: Connectivity, name2: str, conn2: Connectivity, perm_thresholds: List[float]):

    node_angles = np.linspace(0, 180, len(ch_names))
    node_angles[[3, 4]] = [180-205, 205] # TODO: change to swap left and right

    # TODO: set bands globally
    bands = ['Delta', 'Theta', 'Alpha', 'Beta', 'Gamma']
    
    n_bands = len(bands)
    n_col = math.ceil(math.sqrt(n_bands))
    n_row = math.ceil(n_bands / n_col)
    fig, axs = plt.subplots(n_row, n_col)
    
    for band_idx, band_name in enumerate(bands):
        
        conn_diff = conn1.get_data(output='dense')[:, :, band_idx] - conn2.get_data(output='dense')[:, :, band_idx]
        # TODO: refactor to make more clear
        perm_th = list(perm_thresholds.values())[band_idx]
        conn_diff[np.abs(conn_diff) < perm_th] = 0 # Ideally, delete non-sig conns instead and change below plot to 1D array
        n_sig = len(conn_diff[conn_diff!=0])
        
        col = int(band_idx % n_col)
        row = int(math.floor(band_idx / n_col))
        ax = axs[row][col]
        
        # TODO: use different color scheme to not mislead with less connectivity
        plot_connectivity_circle(
            conn_diff,
            node_names=ch_names,
            facecolor='grey',
            textcolor='black',
            linewidth=1, 
            colormap='bwr',
            fontsize_names=8,
            padding=2,
            fontsize_colorbar=8,
            title=f'{band_name} {conn_method}, {name1} - {name2}',
            fig=fig,
            # ax is in the docs but throws a TypeError -- I'm on version 0.3 and it does not yet accept ax kwarg
#             ax=ax,
            subplot=(row+1,col+1,1),
            node_angles=node_angles,
            n_lines=n_sig,
        )
plot_conn_diff_circles('J2', seg_connectivity['J2'], 'J1', seg_connectivity['J1'], perm_thresholds)

This appears related to this question, but the solution there involves a hacky workaround I don’t think is implied by the docs (redrawing multiple figures, then examining the Canvas buffer and using the raw data to produce a png).

Hi @szerfas1,

the error you see tells you that in your version of mne_connectivity the plot_connectivity_circle function does not have an ax argument. You say you are on 0.3 version, but the ax argument was added later - you must have been reading the docs for the development version. This is the link to the 0.3 docs for this function:
https://mne.tools/mne-connectivity/stable/generated/mne_connectivity.viz.plot_connectivity_circle.html

1 Like

Thanks Mikolaj. I bet I was on the wrong documentation, but that documentation still suggests subplot should work, right? Am I missing something else?

You were using an ax argument, which does not exist in version 0.3 of the function. The subplot argument is present.

The easiest fix for you to try is to install the dev version of mne_connectivity.

1 Like

Excellent, that works nicely. It may be nice to learn how to do this with the old kwargs for backwards compatibility, but I think I’ll be able to get this to work nicely.