Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Bug]: legend no longer works with itertools.chain #30078

Closed
@jonnybolton16

Description

@jonnybolton16

Bug summary

Callingplt.legend(...) with arguments of type<class 'itertools.chain'> no longer works, and raisesTypeError: object of type 'itertools.chain' has no len(). This used to work - some update to the source code seems to have caused this bug.

Code for reproduction

importmatplotlib.pyplotaspltimportnumpyasnpimportitertoolsdefflip(items,ncol):returnitertools.chain(*[items[i::ncol]foriinrange(ncol)])x=np.arange(-2*np.pi,2*np.pi,0.1)ax=plt.subplot(111)ax.plot(x,np.sin(x),label='Sine')ax.plot(x,np.cos(x),label='Cosine')ax.plot(x,np.arctan(x),label='Inverse tan')handles,labels=ax.get_legend_handles_labels()plt.legend(flip(handles,2),flip(labels,2),loc=9,ncol=2)plt.grid('on')plt.show()

Actual outcome

Traceback (most recent call last):
File "", line 1, in
File "C:\Users\jonny\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\pyplot.py", line 3628, in legend
return gca().legend(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jonny\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\axes_axes.py", line 337, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jonny\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\matplotlib\legend.py", line 462, ininit
if len(handles) < 2:
^^^^^^^^^^^^
TypeError: object of type 'itertools.chain' has no len()

Expected outcome

For the legend to be correctly applied, as previously.

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.3

Matplotlib Backend

tkagg

Python version

No response

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp