Skip to content

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

Description

@eudoxos

Bug report

Sketch transformation has identical pseudo-random output for identical inputs

The sketch transformation (such as used in xkcd) produces exactly the same randomized result if the same path is plotted repeatedly. This is visible especially in the legend, where the rectangle patch is repeated for each item. I exaggerated randomness and scale to make the effect better visible.

As far as I can see, the culprit is in the built-in RNG which gets re-seeded to 0 for each Sketch instantionation:

.

import matplotlib.pyplot as plt
with plt.xkcd(randomness=40,scale=1):
    import matplotlib
    from matplotlib import gridspec
    pat,txt=plt.pie([10,20,30,40],wedgeprops={'edgecolor':'black'})
    plt.legend(pat,['first','second','third','fourth'],loc='best')

Actual outcome
image

zoom on the legend:

image

Expected outcome

Shapes are distored randomly, e.g. each rectangle in the legend is different.

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.6.7
  • Jupyter version (if applicable): [none]
  • Other libraries:

installation: default packaged matplotlib installation (python3-matplotlib).

Activity

  1. changed the title [-]sketch RNG is seeded the same for each path[/-] [+]sketch RNG is seeded to 0 for each path, making path randomness repetitive[/+] on Feb 21, 2019
  2. changed the title [-]sketch RNG is seeded to 0 for each path, making path randomness repetitive[/-] [+]path randomness is repetitive (sketch RNG seeded to 0 for each path anew)[/+] on Feb 21, 2019
  3. WeatherGod commented on Feb 21, 2019

    @WeatherGod
    Member
  4. modified the milestones: v3.1.0, v3.2.0 on Feb 21, 2019
  5. timhoffm commented on Feb 21, 2019

    @timhoffm
    Member

    Ideally, we would seed once per figure. That would make figures reproducible, but repeated elements within the figure random. Didn‘t check the code if that‘s easily possible.

  6. ImportanceOfBeingErnest commented on Feb 21, 2019

    @ImportanceOfBeingErnest
    Member

    Note that if you seed once per figure the shape of a patch would depend on how many patches you have drawn beforehands.

  7. timhoffm commented on Feb 21, 2019

    @timhoffm
    Member

    Yes, but if I create the same figure with the same code twice, it's the same.

  8. jklymak commented on Feb 21, 2019

    @jklymak
    Member

    seeding once would be good, and grabbing the python seed would be even better if that is at all possible.

  9. eudoxos commented on Feb 22, 2019

    @eudoxos
    Author

    Would it be acceptable to have the seed as a static variable inside the Sketch class, increment it every time its ctor is called (so that it is seeded with 0 for the first instance, 1 for the next one etc), plus somehow expose a python function to reset the seed to 0, which would be called from Figure ctor?

  10. ImportanceOfBeingErnest commented on Feb 22, 2019

    @ImportanceOfBeingErnest
    Member

    As pointed out above, I would not consider this acceptable. There needs to be a way to ensure that a shape is the same, independent of how many other shapes there are drawn before in that figure.

  11. eudoxos commented on Feb 22, 2019

    @eudoxos
    Author

    As pointed out above, I would not consider this acceptable. There needs to be a way to ensure that a shape is the same, independent of how many other shapes there are drawn before in that figure.

    If you look at the legend which I posted above, it is exactly this shape is the same which makes it so ugly and computerish, that's the subject of the issue.

    If there is no general agreement, there must be a setting (in rcParams perhaps?) for that. The options are (not all necessarily to implemented, I am just writing out what is theoretically possible):

    1. seed with a constant: same shape looks the same, current situation; or
    2. seed with a changing seed, resetting to some intial value for each figure: same shape looks different in one figure, but two identical consecutive figures will look the same; or
    3. seed with a changing seed, never resetting (but starting with some given value when matplotlib is imported): nothing will look the same, but running Python more times will always give the same result; or
    4. seed with intiially random (e.g. time since epoch or such) and then changing seed: nothing every the same.

    What I reported (as an issue) is 1. To me at least, 2. is a compromise between per-figure reproducibility (e.g. for testing) and random appearance.

  12. ImportanceOfBeingErnest commented on Feb 22, 2019

    @ImportanceOfBeingErnest
    Member

    The only option I see is to let _sketch take an additional value seed in

    def set_sketch_params(self, scale=None, length=None, randomness=None):

    that will need to be (converted to) a valid seed before passing onto the C level. The C class needs to take this further argument.
    With this being implemented you can already loop though all your artists and set some random or defined seed. Of course any additional features can be built on top, like per-figure-seed or whatever you like. Also for the xkcd style one can sure discuss which of those would make people most happy.

  13. jklymak commented on Feb 22, 2019

    @jklymak
    Member

    I think @ImportanceOfBeingErnest has the correct solution. Then unti tests can set the seed in python, as they already do many times in the tests.

  14. 7 remaining items

  15. added this to the v3.4.0 milestone on May 7, 2020
  16. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  17. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  18. modified the milestones: v3.6.0, v3.7.0 on Jul 8, 2022
  19. story645 commented on Apr 20, 2023

    @story645
    Member

    @eudoxos sorry you never got a response, any chance you're still interested in adding a seed parameter?

  20. eudoxos commented on Apr 21, 2023

    @eudoxos
    Author

    Hi, check out the branch https://github.com/eudoxos/matplotlib/tree/sketch-seed . I don't have resources to re-apply to the current matplotlib (though I think it could be quite straightforward), test, document etc to push it all way to merging. Please feel welcome to build upon what I did.

  21. github-actions commented on Apr 18, 2025

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  22. added
    status: inactive
    on Apr 18, 2025
  23. added
    keep
    and removed
    status: inactive
    on Apr 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keep

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions