You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking at the docstring, this is actually the expected behavior and hence not a bug:
axesinstance of Axes | list of Axes | None
The axes to plot to. If None, a new Figure will be created with the correct number of axes. If Axes are provided (either as a single instance or a list of axes), the number of axes provided must match the number of times provided (unless times is None).Default is None.
Since you pass a multi-dimensional array and not a list-like object, the method fails.
The only bug I can see here is that the error message that is produced is not super helpful – we're obviously failing too late.
I do appreciate your efforts in #11640 though, but you're actually adding a new functionality there (supporting NDarray input), so it's not a bug fix and would also require you to rework the docstring a bit.
Please also note that Matplotlib uses super unhelpful terminology in that "Axes" is the singular form. Multiple Axes, as returned e.g. by plt.subplots(2, 2), are … I don't know, Axess or so. – For that reason, I usually call the singular just ax and if I have multiple, I name the variable axs. Maybe this added to your confusion?
Edit:
If you flatten the provided ax, it fails with:
UnboundLocalError: local variable 'fig' referenced before assignment
To me if list of axes works then 1D ndarray of axes should also work, especially since it's what's returned by plt.subplots for example. Also the fig variable referenced before assignment seems like a(nother) genuine bug to me
The UnboundLocalError is definitely a bug, it's also present if you pass a list of axes. But you are right that at the moment the docstring says List of Axes. The same docstring also mentions times for ICA topographic maps.. 😅
My reasoning would be that an axes argument (or its plural form) should support any output from a matplotlib function that creates axes, as long as the correct number of subplots is provided.
Sure, you can create them one by one, e.g.
from matplotlib import pyplot as plt
f = plt.Figure()
axes = list()
for k in range(4):
axes.append(f.add_subplot(2, 2, k + 1))
But you can also create them all at once with plt.subplots which returns always a 2D array of axes, except if squeeze=True (the default). IMO, output from plt.subplots should be supported out of the box by an axes argument which can take multiple subplots.
It's difficult to make one axes docstring for all MNE-function, but for ica.plot_components, the docstring for axes would become:
axes : Axes | array of Axes | None
Either a single Axes or an iterable of Axes if more than one subplot
is needed. The number of subplots must match the number of
selected components. If None, a new figure will be created
The subplot(s) to plot to. The number of subplots must match the
number of selected components. If None, new figures will be created
with the number of subplots per figure controlled by ``nrows`` and ``ncols``
It's not complete as the types at the top don't mention iterable to avoid lengthening the line to:
axes : Axes | array of Axes | iterable of Axes | None
And it does not mention the 1D / 2D ndarray, but I think that would be adding too much detail.
MO, output from plt.subplots should be supported out of the box by an axes argument which can take multiple subplots.
I generally agree, but how would you determine the order in which to fill the axess? Is left -> right from top to bottom what users would expect? (I'd say: yes, but I'm not sure if everybody would agree?)
But @mscheltienne passed a 2d array of axes in the example
Ahh I missed that!
I generally agree, but how would you determine the order in which to fill the axess (see what I did :D)? Is left -> right from top to bottom what users would expect? (I'd say: yes, but I'm not sure if everybody would agree?)
I think it's pretty clear by the default C-ordering of NumPy arrays that we will just .ravel() / .flatten() the array, which means it will go across the first row left to right, then the second row left to right, etc. If people want something else they can pass axes.T or explicitly order them.
I think it's pretty clear by the default C-ordering of NumPy arrays that we will just .ravel() / .flatten() the array, which means it will go across the first row left to right, then the second row left to right, etc. If people want something else they can pass axes.T or explicitly order them.
Exactly my thought. We define a default, and if they want a different order, they either provide it explicitly in a 1D array or transpose it.
Activity
hoechenberger commentedon Apr 24, 2023
Looking at the docstring, this is actually the expected behavior and hence not a bug:
Since you pass a multi-dimensional array and not a list-like object, the method fails.
The only bug I can see here is that the error message that is produced is not super helpful – we're obviously failing too late.
I do appreciate your efforts in #11640 though, but you're actually adding a new functionality there (supporting
NDarrayinput), so it's not a bug fix and would also require you to rework the docstring a bit.Please also note that Matplotlib uses super unhelpful terminology in that "Axes" is the singular form. Multiple Axes, as returned e.g. by
plt.subplots(2, 2), are … I don't know, Axess or so. – For that reason, I usually call the singular justaxand if I have multiple, I name the variableaxs. Maybe this added to your confusion?Edit:
Okay this bit does seem like a bug to me.
larsoner commentedon Apr 24, 2023
To me if
list of axesworks then1D ndarray of axesshould also work, especially since it's what's returned byplt.subplotsfor example. Also thefigvariable referenced before assignment seems like a(nother) genuine bug to memscheltienne commentedon Apr 24, 2023
The
UnboundLocalErroris definitely a bug, it's also present if you pass a list of axes. But you are right that at the moment the docstring saysList of Axes. The same docstring also mentionstimesfor ICA topographic maps.. 😅My reasoning would be that an
axesargument (or its plural form) should support any output from amatplotlibfunction that creates axes, as long as the correct number of subplots is provided.Sure, you can create them one by one, e.g.
But you can also create them all at once with
plt.subplotswhich returns always a 2D array of axes, except ifsqueeze=True(the default). IMO, output fromplt.subplotsshould be supported out of the box by anaxesargument which can take multiple subplots.It's difficult to make one
axesdocstring for all MNE-function, but forica.plot_components, the docstring foraxeswould become:It's not complete as the types at the top don't mention iterable to avoid lengthening the line to:
And it does not mention the 1D / 2D
ndarray, but I think that would be adding too much detail.hoechenberger commentedon Apr 24, 2023
@larsoner
But @mscheltienne passed a 2d array of
axesin the example@mscheltienne
I generally agree, but how would you determine the order in which to fill the axess? Is left -> right from top to bottom what users would expect? (I'd say: yes, but I'm not sure if everybody would agree?)
larsoner commentedon Apr 24, 2023
Ahh I missed that!
I think it's pretty clear by the default C-ordering of NumPy arrays that we will just
.ravel()/.flatten()the array, which means it will go across the first row left to right, then the second row left to right, etc. If people want something else they can passaxes.Tor explicitly order them.mscheltienne commentedon Apr 24, 2023
Exactly my thought. We define a default, and if they want a different order, they either provide it explicitly in a 1D array or transpose it.