You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
path randomness is repetitive (sketch RNG seeded to 0 for each path anew) #13479
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:
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
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
So, originally, when this feature was implemented, we didn't seed the PRNG.
But, some users complained, and we were having trouble unit-testing it. So,
we started seeding the PRNG. Now, I could have sworn we left in a way to
turn off the seeding, but I can't find anything about it at the moment.
On Thu, Feb 21, 2019 at 7:05 AM eudoxos ***@***.***> wrote:
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:
https://github.com/matplotlib/matplotlib/blob/e02f2c5dca0bfd6a83cd0d41db31e48dea9fd14f/src/path_converters.h#L941
.
import matplotlib.pyplot as pltwith 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: image]
<https://user-images.githubusercontent.com/1029876/53167574-90cbe380-35d8-11e9-93f2-596f6771adbf.png>
zoom on the legend:
[image: image]
<https://user-images.githubusercontent.com/1029876/53167608-ac36ee80-35d8-11e9-86c7-d2745ecfb932.png>
*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).
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#13479>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AARy-KCz6BKILcCjw7V4c7bW9ZWmFnbFks5vPouKgaJpZM4bHSql>
.
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.
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?
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.
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):
seed with a constant: same shape looks the same, current situation; or
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
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
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.
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.
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.
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!
Activity
[-]sketch RNG is seeded the same for each path[/-][+]sketch RNG is seeded to 0 for each path, making path randomness repetitive[/+][-]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)[/+]WeatherGod commentedon Feb 21, 2019
timhoffm commentedon Feb 21, 2019
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.
ImportanceOfBeingErnest commentedon Feb 21, 2019
Note that if you seed once per figure the shape of a patch would depend on how many patches you have drawn beforehands.
timhoffm commentedon Feb 21, 2019
Yes, but if I create the same figure with the same code twice, it's the same.
jklymak commentedon Feb 21, 2019
seeding once would be good, and grabbing the python seed would be even better if that is at all possible.
eudoxos commentedon Feb 22, 2019
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?
ImportanceOfBeingErnest commentedon Feb 22, 2019
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.
eudoxos commentedon Feb 22, 2019
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):
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.
ImportanceOfBeingErnest commentedon Feb 22, 2019
The only option I see is to let
_sketchtake an additional valueseedinmatplotlib/lib/matplotlib/artist.py
Line 609 in 1d7494f
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.
jklymak commentedon Feb 22, 2019
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.
7 remaining items
story645 commentedon Apr 20, 2023
@eudoxos sorry you never got a response, any chance you're still interested in adding a seed parameter?
eudoxos commentedon Apr 21, 2023
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.
github-actions commentedon Apr 18, 2025
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!