Title: [Bug]: legend no longer works with itertools.chain · Issue #30078 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: legend no longer works with itertools.chain · Issue #30078 · matplotlib/matplotlib
X Title: [Bug]: legend no longer works with itertools.chain · Issue #30078 · matplotlib/matplotlib
Description: Bug summary Calling plt.legend(...) with arguments of type
Open Graph Description: Bug summary Calling plt.legend(...) with arguments of type
X Description: Bug summary Calling plt.legend(...) with arguments of type <class 'itertools.chain'> no longer works, and raises TypeError: object of type 'itertools.chain' has no len(). This...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/30078
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: legend no longer works with itertools.chain","articleBody":"### Bug summary\n\nCalling `plt.legend(...)` with arguments of type `\u003cclass 'itertools.chain'\u003e` no longer works, and raises `TypeError: object of type 'itertools.chain' has no len()`. This used to work - some update to the source code seems to have caused this bug.\n\n### Code for reproduction\n\n```Python\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport itertools\n\ndef flip(items, ncol):\n return itertools.chain(*[items[i::ncol] for i in range(ncol)])\n\nx = np.arange(-2*np.pi, 2*np.pi, 0.1)\nax = plt.subplot(111)\nax.plot(x, np.sin(x), label='Sine')\nax.plot(x, np.cos(x), label='Cosine')\nax.plot(x, np.arctan(x), label='Inverse tan')\n\nhandles, labels = ax.get_legend_handles_labels()\nplt.legend(flip(handles, 2), flip(labels, 2), loc=9, ncol=2)\n\nplt.grid('on')\nplt.show()\n```\n\n### Actual outcome\n\nTraceback (most recent call last):\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\n 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\n return gca().legend(*args, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n 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\n self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"C:\\Users\\jonny\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\matplotlib\\legend.py\", line 462, in __init__\n if len(handles) \u003c 2:\n ^^^^^^^^^^^^\nTypeError: object of type 'itertools.chain' has no len()\n\n### Expected outcome\n\nFor the legend to be correctly applied, as previously.\n\n### Additional information\n\n_No response_\n\n### Operating system\n\n_No response_\n\n### Matplotlib Version\n\n3.10.3\n\n### Matplotlib Backend\n\ntkagg\n\n### Python version\n\n_No response_\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\npip","author":{"url":"https://github.com/jonnybolton16","@type":"Person","name":"jonnybolton16"},"datePublished":"2025-05-19T14:46:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/30078/matplotlib/issues/30078"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:68b2aa63-3302-1edf-7ae3-a0e426101f14 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4B6:1668CD:3E34A7:52F401:6A529233 |
| html-safe-nonce | 51e873034424aa5f1d03d0f499885b563ed9786e6d5e67879a4485699a035d92 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEI2OjE2NjhDRDozRTM0QTc6NTJGNDAxOjZBNTI5MjMzIiwidmlzaXRvcl9pZCI6IjY1NjE1NzIzNzQ4Mzk0NjQ0OTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d126136ef3923b549aef7673cc309ee437f4c37cbc6dc48986ade481ac55440c |
| hovercard-subject-tag | issue:3074108680 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/30078/issue_layout |
| twitter:image | https://opengraph.githubassets.com/99acf0f4f5afdba61381a820a7453335cb646695c4235ed94adbffe7087ec03b/matplotlib/matplotlib/issues/30078 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/99acf0f4f5afdba61381a820a7453335cb646695c4235ed94adbffe7087ec03b/matplotlib/matplotlib/issues/30078 |
| og:image:alt | Bug summary Calling plt.legend(...) with arguments of type |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jonnybolton16 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width