# Get colorbar info from plot\_connectivity\_circle

**URL:** <https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715>\
**Category:** Support & Discussions\
**Tags:** visualization\
**Created:** [April 15, 2022, 7:55am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715 "2022-04-15T07:55:05Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [April 15, 2022, 7:55am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/1 "2022-04-15T07:55:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [April 15, 2022, 10:41am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/2 "2022-04-15T10:41:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [April 15, 2022, 6:54pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/3 "2022-04-15T18:54:41Z")

</div>

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!

 ![aaaaa](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/7/729f1c1a5e8d5115c0a524f3b19e46a7822bd754.png)

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [April 15, 2022, 8:17pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/4 "2022-04-15T20:17:45Z")

</div>

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

```python
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!

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [April 15, 2022, 8:44pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/5 "2022-04-15T20:44:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [April 15, 2022, 10:07pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/6 "2022-04-15T22:07:17Z")

</div>

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:

```python
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:

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

```

Or:

```python
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.

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [August 24, 2022, 9:49pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/7 "2022-08-24T21:49:56Z")

</div>

Hi Mathieu,

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

```python
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!

 ![111](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/0/04cb3aee9412bec3d4777cf7f9464ac75fa645fa.png)

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 25, 2022, 9:06am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/8 "2022-08-25T09:06:25Z")

</div>

> [@mscheltienne](#):
>
> `fig.canvas.draw()`

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`).

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [August 25, 2022, 5:14pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/9 "2022-08-25T17:14:34Z")

</div>

Hi Mathieu,

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

```python
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()

```

 ![aaaa](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/e/e39df3797df58829d4ed9623fd705b2cd943e931.png)

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 25, 2022, 5:46pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/10 "2022-08-25T17:46:28Z")

</div>

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.

```python
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:

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/b/bc8f7f462145d46b7cb056dcae8af78e8439f656.png)

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [August 25, 2022, 6:05pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/11 "2022-08-25T18:05:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 25, 2022, 7:04pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/12 "2022-08-25T19:04:09Z")

</div>

Definitely not since I am also a Spyder user.

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 25, 2022, 7:19pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/13 "2022-08-25T19:19:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [August 25, 2022, 8:08pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/14 "2022-08-25T20:08:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caoyq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/caoyq/32/1353_2.png) [@Caoyq](https://mne.discourse.group/u/Caoyq)\
**Post date:** [August 25, 2022, 9:34pm UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/15 "2022-08-25T21:34:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 26, 2022, 7:49am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/16 "2022-08-26T07:49:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [August 26, 2022, 8:48am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/17 "2022-08-26T08:48:09Z")

</div>

> [@mscheltienne](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [August 26, 2022, 9:42am UTC](https://mne.discourse.group/t/get-colorbar-info-from-plot-connectivity-circle/4715/18 "2022-08-26T09:42:22Z")

</div>

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.
