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

BUG: matplotlib 3.4.0 -- Scatter with colormap and legend gives TypeError: object of type 'NoneType' has no len() #19779

Closed
pllim opened this issue Mar 26, 2021 · 7 comments
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Milestone

Comments

@pllim
Copy link

pllim commented Mar 26, 2021

Bug report

Bug summary

With matplotlib 3.4.0, scatter plot with colormap and legend gives TypeError: object of type 'NoneType' has no len().

Code for reproduction

import matplotlib.pyplot as plt
y = [1, 2, 3, 4, 5]
plt.scatter(y, y, label='Y', lw=0, s=8, c=y, cmap='viridis')
plt.legend()
plt.show()

Actual outcome

Traceback (most recent call last):
  File ".../matplotlib/backends/backend_qt5.py", line 475, in _draw_idle
    self.draw()
  File ".../matplotlib/backends/backend_agg.py", line 406, in draw
    self.figure.draw(self.renderer)
  File ".../matplotlib/artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File ".../matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File ".../matplotlib/figure.py", line 2734, in draw
    mimage._draw_list_compositing_images(
  File ".../matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File ".../matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File ".../matplotlib/_api/deprecation.py", line 431, in wrapper
    return func(*inner_args, **inner_kwargs)
  File ".../matplotlib/axes/_base.py", line 2925, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File ".../matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File ".../matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File ".../matplotlib/legend.py", line 614, in draw
    self._legend_box.draw(renderer)
  File ".../matplotlib/offsetbox.py", line 368, in draw
    c.draw(renderer)
  File ".../matplotlib/offsetbox.py", line 368, in draw
    c.draw(renderer)
  File ".../matplotlib/offsetbox.py", line 368, in draw
    c.draw(renderer)
  [Previous line repeated 1 more time]
  File ".../matplotlib/offsetbox.py", line 694, in draw
    c.draw(renderer)
  File ".../matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File ".../matplotlib/collections.py", line 1009, in draw
    super().draw(renderer)
  File ".../matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File ".../matplotlib/collections.py", line 380, in draw
    len(facecolors) == 1 and len(edgecolors) == 1 and
TypeError: object of type 'NoneType' has no len()

Expected outcome

With matplotlib 3.3.4, I get this (no warning or error):

Screenshot 2021-03-26 141815

Matplotlib version

  • Operating system: Red Hat Enterprise Linux Server release 7.9 (Maipo)
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.0
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.8.3
  • Jupyter version (if applicable): N/A
  • Other libraries: Not sure what else you need, just let me know.
@smartlixx
Copy link
Contributor

smartlixx commented Mar 27, 2021

A bisect diagnose shows that the problem was introduced in the commit
4c3d644 (#18480), which changed the way Collections deal with color.

A quick fix is easy, but I don't have much in-depth understanding of this part of the code. If anyone familiar with this part can have a discussion, that would be great. Or I just open a PR for discussion.

@dstansby
Copy link
Member

Opening a PR is probably the best way forward 👍

@tacaswell tacaswell added this to the v3.4.1 milestone Mar 28, 2021
@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Mar 28, 2021
@smartlixx
Copy link
Contributor

smartlixx commented Mar 29, 2021

A bisect diagnose shows that the problem was introduced in the commit
4c3d644 (#18480), which changed the way Collections deal with color.

A quick fix is easy, but I don't have much in-depth understanding of this part of the code. If anyone is familiar with this part can have a discussion, that would be great. Or I just open a PR for discussion.

Actually, this problem has been solved in the commit ed6d92b (#19552), but this commit was not included in v3.4.x branch. Backport that commit will solve this problem.

@QuLogic
Copy link
Member

QuLogic commented Mar 29, 2021

That commit removes API and cannot be included in v3.4.x directly.

@QuLogic
Copy link
Member

QuLogic commented Mar 29, 2021

Maybe we should just drop the self._check_update('array') though, @efiring?

QuLogic added a commit to QuLogic/matplotlib that referenced this issue Mar 29, 2021
This is a slight backport of ed6d92b,
namely ignoring the array update status when updating the scalar
mappable.

To not break the watchers though, the check must still be called to
update the status.

Fixes matplotlib#19779.
QuLogic added a commit to QuLogic/matplotlib that referenced this issue Mar 30, 2021
These were added in matplotlib#18480, and not copying them can mean that an artist
that used `update_from` could get in an inconsistent state.

For example, this will fix legends of colour-mapped scatter plots, where
a copy of the scatter Collection is made to go in the legend.

Fixes matplotlib#19779.
@QuLogic
Copy link
Member

QuLogic commented Mar 31, 2021

Closed by #19816.

@QuLogic QuLogic closed this as completed Mar 31, 2021
@pllim
Copy link
Author

pllim commented Mar 31, 2021

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

No branches or pull requests

5 participants