Get colorbar info from plot_connectivity_circle

Hi all,

Does anyone know how to get colorbar information from the figures of plot_connectivity_circle? I struggled with this for a long time! I wanna add colorbar title and edit the ticks.

THX!

Hello, this function should return a matplotlib figure in which you can find the axes attribute. One of fig.axes should be the colobar. Can you test this?

Hi Mathies,

THX for your instruction!

Yes, I found the attribute via the following code:

from mne_connectivity.viz import plot_connectivity_circle as pcc
fig, _ = pcc(corr, Lbl, linewidth=2, fontsize_names=28)
fig.axes[1]._colorbar.set_ticks=[-1, 0, 1]

However, the set function doesn’t work, the colorbar is not configured as what I want, do you know why? THX!

I’m going to go for a guess here, you only did this:

from mne_connectivity.viz import plot_connectivity_circle as pcc
fig, _ = pcc(corr, Lbl, linewidth=2, fontsize_names=28)
fig.axes[1]._colorbar.set_ticks=[-1, 0, 1]

Thus, the figure got shown when the second line is executed. You should close this figure and show it again after resetting the ticks. Alternatively to closing the figure shown by the second line, you can use the argument show=False as follows: fig, _ = pcc(corr, Lbl, linewidth=2, fontsize_names=28, show=False).

To show the figure, I’m guessing fig.show() or plt.show() (with from matplotlib import pyplot as plt).

Let me know!

Hi Mathieu,

THX for your info!

Unfortunately, the figure even shows after setting the option as False. Thus, the colorbar tick is still not good.

Hmm OK. For show=False, it might be a bug.

For the update of the color bar tick, it’s really a matplotlib problem more than an MNE problem. First, what you want to update is the yticklabels. Second, after updating the tick labels, you also need to redraw the canvas before showing the figure.

So, something like this:

fig.axes[1].set_yticklabels(['1', '2', '3', '4', '5'])
fig.canvas.draw()
fig.show()

I used an example from MNE connectivity, and 3 ticks had a label and were displayed, but there were actually 5 ticks stored. To figure out if this is the case for you, you can retrieve the ticks with:

cb_yticks = plt.getp(fig.axes[1], 'yticklabels')

Or:

fig.axes[1].get_yticklabels()

One last comment, you should avoid accessing private attributes from classes as you did here: fig.axes[1]._colorbar.set_ticks=[-1, 0, 1]. Private attributes start with a _ and you might break things if you start updating those directly.

Hi Mathieu,

Unfortunately, the following codes still don’t work even I set the yticks and yticklabels:

chord = plt.figure(figsize=(9,9)); 
cd = pcc(corr, Lbl, fig=chord, colormap='bwr', colorbar_size=0.9, fontsize_colorbar=18,\
        vmin=-1, vmax=1, linewidth=2, fontsize_names=30, fontsize_title=20, facecolor='white',\
        title=land + ' ' + pol + ' on ' + day + ' ' + MST + ' (MST)', textcolor='black', node_width=0);
chord.axes[1].set_yticks([-1,0,1]); chord.axes[1].set_yticklabels(['-1','0','1']);

Could you please help me? THX!

This is not optional, and in your case it would be chord.canvas.draw(). If that does not work, can you provide a short reproducible example, that is a short code snippet I can copy/paste and run without further changes which demonstrate the issue. To create it, you can copy/paste part of a tutorial (especially the loading which loads datasets from mne.datasets).

Hi Mathieu,

Try the following code, it can run, but still the colorbar ticks are not cutomized.

import numpy as np; import matplotlib.pyplot as plt;
from mne_connectivity.viz import plot_connectivity_circle as pcc

chord = plt.figure(figsize=(9,9))
cd, cx = pcc(np.corrcoef(np.random.rand(3,3)),  [r'$\theta$','SWE','VMC',], fig=chord, colormap='bwr', colorbar=True, textcolor='black', colorbar_size=0.9, fontsize_colorbar=18, vmin=-1, vmax=1, linewidth=2, \
         fontsize_names=30, fontsize_title=20, facecolor='white', node_width=0);
chord.axes[1].set_yticks([-1,0,1]); chord.axes[1].set_yticklabels(['-1','0','1']);
chord.canvas.draw(); chord.show()

I copy pasted your code and expanded it to avoid the ; and multiple arguments on a single line (it’s just a style preference for readability), and… it works.

import matplotlib.pyplot as plt
import numpy as np
from mne_connectivity.viz import plot_connectivity_circle as pcc


chord = plt.figure(figsize=(9,9))
cd, cx = pcc(
    np.corrcoef(np.random.rand(3,3)), 
    [r'$\theta$','SWE','VMC',], 
    fig=chord, 
    colormap='bwr', 
    colorbar=True, 
    textcolor='black', 
    colorbar_size=0.9, 
    fontsize_colorbar=18, 
    vmin=-1, 
    vmax=1, 
    linewidth=2, \
    fontsize_names=30, 
    fontsize_title=20, 
    facecolor='white', 
    node_width=0,
)
chord.axes[1].set_yticks([-1,0,1])
chord.axes[1].set_yticklabels(['-1','0','1'])
chord.canvas.draw() 
chord.show()

With mne-connectivity v0.3:

Unbelievable! I ran my code on Spyder, do you think that’s the platform issue?

Definitely not since I am also a Spyder user.

Just in case, in Spyder, go to settings, IPython console, Graphics, and set the backend to Qt5. Maybe you are using inline, and maybe it does not work for inline plots.

OMG! It finally works! THX a lot! You are the ultimate savior!

One additional note: even the inline figure did not have the specific colorbar settings, but when you save the figure, the configuration works! WEIRD!

Not that weird, I guess you can’t update the canvas of an inline figure while saving will re-render and update the figure anyway.

Wouldn’t the “fix” then be to simply display / show() the figure again?

Apparently not since he did have a fig.show() at the end of his code after re-drawing the canvas.
I don’t use inline plotting in Spyder, it’s way too limited compared to Qt, so I don’t really know the reason why it does not work.