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

Add option to set colorbar ticks and label in plot_connectivity_circle #262

Closed
ruuskas opened this issue Dec 2, 2024 · 7 comments · Fixed by mne-tools/mne-python#13019 · May be fixed by #266
Closed

Add option to set colorbar ticks and label in plot_connectivity_circle #262

ruuskas opened this issue Dec 2, 2024 · 7 comments · Fixed by mne-tools/mne-python#13019 · May be fixed by #266

Comments

@ruuskas
Copy link
Contributor

ruuskas commented Dec 2, 2024

Describe the problem

The connectivity circle visualization function currently does not allow much control over the colorbar. I think many users might want to set a label for the colorbar and control the tick locations to produce figures closer to publication quality. It appears somewhat difficult to change these and find the colorbar instance after the figure is created.

Describe your solution

It's very simple of course. I can make a PR adding a few parameters to the plotting function here and in mne.viz.circle if this is something others consider valuable. Perhaps something like colorbar_ticks and colorbar_label.

@larsoner
Copy link
Member

larsoner commented Dec 2, 2024

I am more inclined to teach users how to find a colorbar for any matplotlib figure, since there are lots of things you might want to set (title, ticks, spacing, etc.). Did you find any reasonable / reliable way to get the colorbar axes? Maybe it just lives in fig.axes[-1]?

@ruuskas
Copy link
Contributor Author

ruuskas commented Dec 3, 2024

It does live in fix.axes[-1], but getting the Colorbar object out of the Axes does not seem trivial. Perhaps I'm not good enough at Googling, but I'm failing to find a way to manipulate it after no longer having a direct pointer to the colorbar.

I agree that it might be worth it expanding the tutorials. I don't know what would be a reasonable level though. There are quite many steps to make figures like the one below. My workaround to manipulate the colorbar has been to just make the necessary tweaks to plot_connectivity_circle locally.

Perhaps we could add a colorbar_kwargs parameter to avoid cluttering the API?

example_con

@larsoner
Copy link
Member

larsoner commented Dec 3, 2024

Agreed it's not easy to find. At least for images you can do for example:

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> fig, ax = plt.subplots(layout="constrained")
>>> ax.imshow(np.random.default_rng(0).normal(size=(10, 12)))
<matplotlib.image.AxesImage object at 0x755babec0ef0>
>>> cb1 = fig.colorbar(ax.images[0])
>>> ax.images[-1].colorbar
<matplotlib.colorbar.Colorbar object at 0x755babec2750>

But no idea if that's the case here.

Perhaps we could add a colorbar_kwargs parameter to avoid cluttering the API?

That would be okay. Another option that might be even better (?) would be to add a fig.mne which is just a Bunch object. And we can have a fig.mne.colorbar. We do this sort of thing to keep track of stuff in our matplotlib-based figures sometimes. It seems a bit more flexible than the colorbar_kwargs because then you have access to all methods of the colorbar itself, which is nice.

@ruuskas
Copy link
Contributor Author

ruuskas commented Dec 4, 2024

At least for images you can do for example

This approach doesn't seem to work for the PolarAxes, or at least I could not find the colorbar while looking into what's inside fig.axes[0].

That would be okay. Another option that might be even better (?) would be to add a fig.mne which is just a Bunch object. And we can have a fig.mne.colorbar. We do this sort of thing to keep track of stuff in our matplotlib-based figures sometimes. It seems a bit more flexible than the colorbar_kwargs because then you have access to all methods of the colorbar itself, which is nice.

This sounds good to me. I can test whether it works as expected and then make a PR. I think it's necessary to somehow indicate in the docstring of plot_connectivity_circle that this exists so that others can find it.

@larsoner
Copy link
Member

larsoner commented Dec 4, 2024

I think it's necessary to somehow indicate in the docstring of plot_connectivity_circle that this exists so that others can find it.

Sure I think a sentence in the Notes section plus maybe an updated example that made use of it would be good!

@drammock
Copy link
Member

drammock commented Dec 6, 2024

I can test whether it works as expected and then make a PR

Question for @larsoner about the implementation: why Bunch instead of, e.g., SimpleNamespace or DataClass? Is it somehow important that both .attr and ["attr"] access work?

@larsoner
Copy link
Member

larsoner commented Dec 6, 2024

Those would probably be better, I just forget about them!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants