Skip to content

Add path.sketch_seed to control sketch randomness#31311

Open
Aryan-Gore wants to merge 11 commits intomatplotlib:mainfrom
Aryan-Gore:sketch-seed-work
Open

Add path.sketch_seed to control sketch randomness#31311
Aryan-Gore wants to merge 11 commits intomatplotlib:mainfrom
Aryan-Gore:sketch-seed-work

Conversation

@Aryan-Gore
Copy link
Copy Markdown

@Aryan-Gore Aryan-Gore commented Mar 15, 2026

PR Summary

This PR adds a configurable seed for the sketch/xkcd path randomness,
fixing issue #13479 where all paths used seed 0 causing repetitive patterns.

Closes #13479
Rebased from #26050 (original work by @AryanSheka, @oscargus, @eudoxos)

Changes

  • Adds path.sketch_seed to rcParams
  • Fixed SketchParams pybind11 type caster to handle 4-tuple
  • Fixed Sketch constructor calls in _backend_agg.h to pass seed
  • Added test_sketch_rolling_seed for deterministic behavior

Checklist

@story645
Copy link
Copy Markdown
Member

story645 commented Mar 15, 2026

Thanks for tackling this, please add @AryanSheka as a co-author

@Aryan-Gore
Copy link
Copy Markdown
Author

Aryan-Gore commented Mar 16, 2026

Thanks for tackling this, please add @AryanSheka as a co-author
added @AryanSheka as co-author

Seed can be manually set for path.sketch by modifying the value of rcParams path.sketch_seed or by passing a seed value to xkcd or Artist.set_sketch . Seed will also have a rolling(auto incrementing) behaviour.

Co-Authored-By: Oscar Gustafsson <8114497+oscargus@users.noreply.github.com>
Co-Authored-By: eudoxos <1029876+eudoxos@users.noreply.github.com>

Co-authored-by: AryanSheka
@Aryan-Gore
Copy link
Copy Markdown
Author

Thanks for tackling this, please add @AryanSheka as a co-author
added @AryanSheka as co-author

Hi @story645, I've added a test for deterministic sketch seed behavior (test_sketch_rolling_seed) — verifying that the same seed produces identical output every time. All 7 sketch/xkcd tests are now passing. Please let me know if there's anything else needed

@melissawm melissawm mentioned this pull request Mar 17, 2026
5 tasks
@story645 story645 marked this pull request as draft March 17, 2026 18:47
@story645
Copy link
Copy Markdown
Member

story645 commented Mar 17, 2026

Hi, so the C++ isn't compiling and that's why all the tests are failing. I'm gonna convert this to draft, please make sure the code compiles before submitting it for review. If you're struggling w/ the compilation, please ask questions.

Also please fill out the pull request template:
https://github.com/matplotlib/matplotlib/blob/main/.github/PULL_REQUEST_TEMPLATE.md

@Aryan-Gore
Copy link
Copy Markdown
Author

Hi, so the C++ isn't compiling and that's why all the tests are failing. I'm gonna convert this to draft, please make sure the code compiles before submitting it for review. If you're struggling w/ the compilation, please ask questions.

Also please fill out the pull request template: https://github.com/matplotlib/matplotlib/blob/main/.github/PULL_REQUEST_TEMPLATE.md

Hi @story645, I've pushed the fix for the C++ compilation issue (Sketch constructor calls now pass seed argument in _backend_agg.h). All 7 sketch/xkcd tests are passing locally. I've also filled out the PR template. Please let me know if anything else is needed!

@story645
Copy link
Copy Markdown
Member

test failures are related to the PR, please either debug or ask questions if you're stuck

@Aryan-Gore
Copy link
Copy Markdown
Author

test failures are related to the PR, please either debug or ask questions if you're stuck

Hi @story645, I've pushed all the fixes — SketchParams type caster now handles 4-tuple correctly with proper indentation, and Sketch constructor calls pass seed in _backend_agg.h. All 7 tests passing locally.

@story645
Copy link
Copy Markdown
Member

story645 commented Mar 19, 2026

All 7 tests passing locally.

please make sure they also pass on CI. If you haven't yet, install the pre-commit hooks to help with the linting errors.

@Aryan-Gore
Copy link
Copy Markdown
Author

please make sure they also pass on CI. If you haven't yet, install the pre-commit hooks to help with the linting errors.

ok

@Aryan-Gore
Copy link
Copy Markdown
Author

Aryan-Gore commented Mar 19, 2026

please make sure they also pass on CI. If you haven't yet, install the pre-commit hooks to help with the linting errors.

ok

i have checkd all but i am not getting these errors.

@Aryan-Gore Aryan-Gore marked this pull request as ready for review March 19, 2026 09:40
@Aryan-Gore
Copy link
Copy Markdown
Author

i am stuck here unable to do it , can you suggest specific changes I should focus on, or would it be better for me to start with a smaller issue first?

@rcomer
Copy link
Copy Markdown
Member

rcomer commented Mar 21, 2026

The remaining test failures are all known to be flaky, so not caused by this PR.

@Aryan-Gore
Copy link
Copy Markdown
Author

Hi @story645,

I’ve addressed all the fixes — compilation, SketchParams type caster, seed handling, and linting. All 7 sketch/xkcd tests pass locally. I noticed some CI checks are still failing, but from the previous discussion, it seems these are known flaky tests.

Could you please advise if there’s anything specific I should focus on next, or if the PR is ready for review despite the CI flakes?

Thanks!
Aryan

m_has_last(false),
m_p(0.0),
m_rand(0)
m_rand(0),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi @story645 @Aryan-Gore . Nice to see that this PR is moving forward. I believe this should be replaced with m_rand(seed), the functionality will not change because with every call of rewind, the seed is set to whatever is passed as argument, but its better to initialize with seed too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've updated the initialization to use the provided seed.Please let me know if you'd like any further adjustments.

Copy link
Copy Markdown
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

To be checked (I don't overlook this right now): Does the addtion of the seed break the backend API and would result in incompatible third-party backends?

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

with plt.xkcd():
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.

can we seed here so that the test images stay the same and don't need to be regenerated?

y2 = 5 - x
y3 = 2.5 * np.ones(8)

with plt.xkcd():
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.

Same: can we seed here so that the test images stay the same and don't need to be regenerated?

# - *randomness* is the factor by which the length is
# randomly scaled.
#path.effects:
#path.sketch_seed: 0 # seed for the internal pseudo number generator.
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.

Move up next to path.sketch.

Is it a conscious decision to make this a separate parameter and not expand path.sketch to a 4-tuple? (Still accepting a 3-tuple for backward compatibility and in the validator adding seed=0). There may be usability aspects to warrant a separate parameter, OTOH it could also be reasonable to collect all sketch related parameters in a single structure; and it handled like this in the rest of the code.

Copy link
Copy Markdown
Member

@story645 story645 Mar 27, 2026

Choose a reason for hiding this comment

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

I think it was mostly to make implementation easier/cause internally it's managed separately from sketch:

#26050 (comment)
https://github.com/matplotlib/ProjectManagement/blob/f61f30f087c17098009bcb57adab0483b9d72a61/meeting_notes/2023/march_july.md?plain=1#L262

else:
self._sketch = (scale, length or 128.0, randomness or 16.0)
self._sketch = (scale, length or 128.0, randomness or 16.0,
self._sketch_seed)
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.

Why do we store the seed twice? self._sketch[3] == self._sketch_seed.

@QuLogic
Copy link
Copy Markdown
Member

QuLogic commented Mar 27, 2026

Please correct the title of this PR; it seems to be one sentence pasted into the middle of another.

@Aryan-Gore
Copy link
Copy Markdown
Author

To be checked (I don't overlook this right now): Does the addtion of the seed break the backend API and would result in incompatible third-party backends?

Thanks for pointing this out.

As far as I understand, the seed only affects the internal randomness of the sketch effect and should not impact backend APIs. However, I’m not fully certain, so I’ll review this more carefully to ensure no incompatibilities with backends.

@Aryan-Gore Aryan-Gore changed the title Added path.sketcFix sketch seed support and add seed to SketchParams type casterh_seed to rcParams Add path.sketch_seed to control sketch randomness Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

path randomness is repetitive (sketch RNG seeded to 0 for each path anew)

6 participants