Skip to content

FIX: scatter with ls="" crashes on PDF savefig#31587

Merged
tacaswell merged 8 commits into
matplotlib:mainfrom
beelauuu:fix-scatter-empty-linestyle-pdf
May 22, 2026
Merged

FIX: scatter with ls="" crashes on PDF savefig#31587
tacaswell merged 8 commits into
matplotlib:mainfrom
beelauuu:fix-scatter-empty-linestyle-pdf

Conversation

@beelauuu

@beelauuu beelauuu commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

PR summary

Closes #31585

Fixes a crash when saving a figure containing a scatter plot with ls="" (or ls=" " / ls="none") to a PDF file. "", " ", and "none" are documented "draw nothing" linestyle aliases, but _get_dash_pattern in lines.py did not recognize them —
it only handled "solid" and "None"

AI Disclosure

This PR was developed with Claude Code for triaging. The root cause analysis, fix location, and test coverage were worked out together.

PR checklist

@dstansby dstansby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good, and I manually checked it fixes the original issue 👍

I have one question about the test before approving.

Comment thread lib/matplotlib/tests/test_collections.py Outdated
Refactor test_scatter_empty_linestyle to use 'pdf' backend only.
@WeatherGod

WeatherGod commented Apr 29, 2026 via email

Copy link
Copy Markdown
Member

@beelauuu

Copy link
Copy Markdown
Contributor Author

Since the bug is in the line collection object, it would make sense to test the _get_dash_pattern() method to make sure it works as expected. The pdf test is useful as an integration test, but the test doesn't explain what exactly it is testing for and why only those parameter values. Do we know that only pdf is affected? What about eps?

On Wed, Apr 29, 2026 at 3:23 PM David Stansby @.> wrote: @.* approved this pull request. — Reply to this email directly, view it on GitHub <#31587 (review)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACHF6ACUZCZVEOCFCW542T4YJJDZAVCNFSM6AAAAACYK4IKFKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHM2DEMBQGA2TSMBVGE . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you are subscribed to this thread.Message ID: @.***>

I can add a unit test for the _get_dash_pattern() for the new cases but it feels redundant given that the existing pdf case wouldn't pass either if _get_dash_pattern() was incorrect.

I verified that the SVG, PS/EPS, and AGG backends don't crash (and had them in the unit test at first; happy to add them back if you feel like it's needed). They skip setting the linewidth on the graphics context because the empty linestyle list causes Nlinewidths=0 in _iter_collection. Meanwhile, PDF hard crashes because it ultimately ends up calling np.max(linewidths).

Let me know your thoughts @WeatherGod

@QuLogic

QuLogic commented May 1, 2026

Copy link
Copy Markdown
Member

So _get_dash_pattern is called in two places: Line2D.set_linestyle and _get_dash_patterns which is called by Collection.set_linestyle. The former normalizes ' ', '' and 'none' to 'None' (which is weirdly the discouraged form, but it's an internal usage, so whatever), but not the latter. The former also verifies against ls_mapper_r and Line2D._lineStyles (which is also a public attribute for whatever reason.) It thus appears that there is lots of possible cleanup to internal handling of line styles (cf #19300), but this may be a sufficient fix in the interim.

@beelauuu

beelauuu commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

Since the bug is in the line collection object, it would make sense to test

the _get_dash_pattern() method to make sure it works as expected. The pdf

test is useful as an integration test, but the test doesn't explain what

exactly it is testing for and why only those parameter values. Do we know

that only pdf is affected? What about eps?

On Wed, Apr 29, 2026 at 3:23 PM David Stansby @.***>

wrote:

@.**** approved this pull request.

Reply to this email directly, view it on GitHub

#31587 (review),

or unsubscribe

https://github.com/notifications/unsubscribe-auth/AACHF6ACUZCZVEOCFCW542T4YJJDZAVCNFSM6AAAAACYK4IKFKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHM2DEMBQGA2TSMBVGE

.

Triage notifications on the go with GitHub Mobile for iOS

https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675

or Android

https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message

ID: @.***>

Interesting, I agree with your take that this is a temporary patch then. Worth a longer conversation for normalizing line styles in the future.

@WeatherGod

WeatherGod commented May 1, 2026 via email

Copy link
Copy Markdown
Member

@beelauuu

beelauuu commented May 2, 2026

Copy link
Copy Markdown
Contributor Author

The test still needs an explanation for what it is testing.

On Fri, May 1, 2026 at 4:38 PM Brian Lau @.> wrote: beelauuu left a comment (matplotlib/matplotlib#31587) <#31587 (comment)> Since the bug is in the line collection object, it would make sense to test the _get_dash_pattern() method to make sure it works as expected. The pdf test is useful as an integration test, but the test doesn't explain what exactly it is testing for and why only those parameter values. Do we know that only pdf is affected? What about eps? On Wed, Apr 29, 2026 at 3:23 PM David Stansby @.> wrote: @.**** approved this pull request. — Reply to this email directly, view it on GitHub #31587 (review) <#31587 (review)> , or unsubscribe https://github.com/notifications/unsubscribe-auth/AACHF6ACUZCZVEOCFCW542T4YJJDZAVCNFSM6AAAAACYK4IKFKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHM2DEMBQGA2TSMBVGE . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub . You are receiving this because you are subscribed to this thread.Message ID: @.> Interesting, I agree with your take that this is a temporary patch then. Worth a longer conversation for normalizing line styles in the future. — Reply to this email directly, view it on GitHub <#31587 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACHF6GYSV43YQ7Z7BTDZSD4YUDNTAVCNFSM6AAAAACYK4IKFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DGNRRGUZDSMJXGA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you were mentioned.Message ID: @.>

Done.

@tacaswell tacaswell added this to the v3.12.0 milestone May 22, 2026
@tacaswell

Copy link
Copy Markdown
Member

Backport at @QuLogic 's choice.

@tacaswell tacaswell merged commit e95ce4b into matplotlib:main May 22, 2026
40 of 41 checks passed
@QuLogic

QuLogic commented May 22, 2026

Copy link
Copy Markdown
Member

Probably can backport.

@meeseeksdev backport to v3.11.x

@QuLogic QuLogic modified the milestones: v3.12.0, v3.11.0 May 22, 2026
tacaswell added a commit that referenced this pull request May 22, 2026
…587-on-v3.11.x

Backport PR #31587 on branch v3.11.x (FIX: scatter with ls="" crashes on PDF savefig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Different behaviour between showing and savefig PDF

6 participants