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
{{ message }}
Repository navigation
[Bug]: PolyCollection set_offsets does not accept [[], []] like Line2D #22401
The return value of plot a Line2D lets you set_data as [[], []] this is nice because you can do both of
line=plt.plot([],[])[0]
line.set_data([[],[]])
allowing you to make a line disappear by setting it's data to empty. This comes up surprisingly often for me when using mpl-interactions to explore datasets
However, plt.scatter does not have the same symmetry of behavior. You can do
changed the title [-][Bug]: PolyCollection `set_offsets` should accept `[[], []]` like Line2D[/-][+][Bug]: PolyCollection `set_offsets` does not accept `[[], []]` like Line2D[/+]on Feb 5, 2022
allowing you to make a line disappear by setting it's data to empty.
If you want to make a line disappear, you should use set_visible(False). But I agree, it's reasonable to allow an artist without data.
For me it's when controlling the artist with a slider. You are moving some parameter around and for some values you may not plot anything. I forget the actual use case by some user of mpl-interactions ran into this. In that context they just provide a function that generates data and I call set_offsets for them it would be a decent bit of extra logic to also manage set_visible just for the case of empty data - especially when it works fine for other artists
"Artist without content" is a reasonable if not necessary concept. It allows us to keep track of a data set, even if we don't have values for it right now.
This is also relevant for legend consistency and color synchronization. See #21506 for a similar case.
Reacted by hannah, Thomas A Caswell and Jody Klymak
I strongly agree with @ianhi and @timhoffm, empty artists are a useful concept: I build up my "ordered stack" of pre-configured scatter plots and dynamically add or remove data and want them completely removed when there is no data to plot - this can be conveniently done with the above empty_offset construct. Furthermore (have not checked it), set_visible(False) might not free the memory of the underlying arrays which might be also of concern when the data is larger.
Looking back I'm not sure I was properly thinking about the shapes because I was comparing set_data and set_offsets which are two distinct things. set_data documents taking (2, N) while set_offsets documents (N,2). This was caught by @tacaswell in #22408 (comment)
So the correct input for an empty offset is not [[],[]] it has to be construct using numpy:
set_offsets(np.asarray([[],[]]).T)
or np.empty((0,2))
I think I got bit confused by the fact that you can do set_offsets([1,1]) (shape (2,)) and it gets interpreted correctly. So I thought a natural extension was [[1,1,1], [1,1,1]] or [[],[]]. but both of those would be wrong
I'm starting to think that the correct solution here is to raise an informative error message before failing on the indexing
edit: which I have now updated the linked PR #22408 to do
Activity
[-][Bug]: PolyCollection `set_offsets` should accept `[[], []]` like Line2D[/-][+][Bug]: PolyCollection `set_offsets` does not accept `[[], []]` like Line2D[/+]timhoffm commentedon Feb 6, 2022
jklymak commentedon Mar 29, 2022
I guess I'm mildly against this - what is the use case to have no data, but keep the artist alive?
ianhi commentedon Mar 29, 2022
For me it's when controlling the artist with a slider. You are moving some parameter around and for some values you may not plot anything. I forget the actual use case by some user of mpl-interactions ran into this. In that context they just provide a function that generates data and I call
set_offsetsfor them it would be a decent bit of extra logic to also manageset_visiblejust for the case of empty data - especially when it works fine for other artiststimhoffm commentedon Mar 29, 2022
"Artist without content" is a reasonable if not necessary concept. It allows us to keep track of a data set, even if we don't have values for it right now.
This is also relevant for legend consistency and color synchronization. See #21506 for a similar case.
oscargus commentedon Jun 4, 2022
I'm wondering if this is (slightly) related to #1735? If so, the proposed fix in #23200 may affect the current issue in a negative(?) way.
pe224 commentedon Apr 15, 2024
I encountered a similar issue and found a workaround by investigating what is returned by
It is a masked (0, 2) array with all values masked, so it can be set via
I strongly agree with @ianhi and @timhoffm, empty artists are a useful concept: I build up my "ordered stack" of pre-configured scatter plots and dynamically add or remove data and want them completely removed when there is no data to plot - this can be conveniently done with the above
empty_offsetconstruct. Furthermore (have not checked it),set_visible(False)might not free the memory of the underlying arrays which might be also of concern when the data is larger.tacaswell commentedon Apr 15, 2024
I agree that supporting this would be good. I suspect that #22408 can be adopted if someone is interested in pushing on this.
ianhi commentedon Dec 12, 2025
Looking back I'm not sure I was properly thinking about the shapes because I was comparing
set_dataandset_offsetswhich are two distinct things.set_datadocuments taking(2, N)whileset_offsetsdocuments(N,2). This was caught by @tacaswell in #22408 (comment)So the correct input for an empty offset is not
[[],[]]it has to be construct using numpy:set_offsets(np.asarray([[],[]]).T)or
np.empty((0,2))I think I got bit confused by the fact that you can do
set_offsets([1,1])(shape(2,)) and it gets interpreted correctly. So I thought a natural extension was[[1,1,1], [1,1,1]]or[[],[]]. but both of those would be wrongI'm starting to think that the correct solution here is to raise an informative error message before failing on the indexing
edit: which I have now updated the linked PR #22408 to do