Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Sketch seed#26050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Sketch seed#26050
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Seed for ``path.sketch`` will have a rolling (auto incrementing) behaviour | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
The seed for the internal Pseudo number generator will now have an auto changing behavior. | ||
This means that the C code of every artist will get a different seed every time it is called | ||
and this will be done in a deterministic manner. | ||
Two figures sketched with the same parameters and different seed will look different from one another. | ||
``Artist.get_sketch_params()`` will now return a 4-tuple instead of a 3-tuple consisting of | ||
(scale, length, randomness, seed) of the form (float, float, float, int). | ||
See 'What's new' on how to set a value to the seed and its behaviour. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
``sketch_seed`` parameter for rcParams | ||
-------------------------------------- | ||
`~matplotlib.rcParams` now has a new parameter ``path.sketch_seed``. | ||
Its default value is 0 and accepted values are any non negative integer. | ||
This allows the user to set the seed for the internal pseudo random number generator in one of three ways. | ||
1) Directly changing the rcParam: | ||
rcParams['path.sketch_seed'] = 20 | ||
2) Passing a value to the new *seed* parameter of `~matplotlib.pyplot.xkcd` function: | ||
plt.xkcd(seed=20) | ||
3) Passing a value to the new *seed* parameter of matplotlib.artist.set_sketch_params function: | ||
ln = plt.plot(x, y) | ||
ln[0].set_sketch_params(seed = 20) | ||
The seed will also have a changing characteristic for every artist which will be done in a deterministic manner. | ||
.. plot:: | ||
:include-source: true | ||
import matplotlib.pyplot as plt | ||
from matplotlib import rcParams | ||
with plt.xkcd(): | ||
rcParams['path.sketch_seed']=0 | ||
rcParams['path.sketch']=(2,120,40) | ||
pat,txt=plt.pie([10,20,30,40],wedgeprops={'edgecolor':'black'}) | ||
plt.legend(pat,['first','second','third','fourth'],loc='best') | ||
plt.title("seed = 0") | ||
plt.show() | ||
.. plot:: | ||
:include-source: true | ||
import matplotlib.pyplot as plt | ||
from matplotlib import rcParams | ||
fig, ax = plt.subplots() | ||
x = np.linspace(0.7, 1.42, 100) | ||
y = x ** 2 | ||
ln = ax.plot(x, y, color='black') | ||
ln[0].set_sketch_params(100, 100, 20, 40) | ||
plt.title("seed = 40") | ||
plt.show() | ||
.. plot:: | ||
:include-source: true | ||
import matplotlib.pyplot as plt | ||
from matplotlib import rcParams | ||
with plt.xkcd(seed=19680801): | ||
import matplotlib | ||
from matplotlib import gridspec | ||
rcParams['path.sketch']=(2,120,40) | ||
pat,txt=plt.pie([10,20,30,40],wedgeprops={'edgecolor':'black'}) | ||
plt.legend(pat,['first','second','third','fourth'],loc='best') | ||
plt.title("seed = 19680801") | ||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -209,6 +209,7 @@ def __init__(self): | ||
self._gid = None | ||
self._snap = None | ||
self._sketch = mpl.rcParams['path.sketch'] | ||
self._sketch_seed = mpl.rcParams['path.sketch_seed'] | ||
self._path_effects = mpl.rcParams['path.effects'] | ||
self._sticky_edges = _XYPair([], []) | ||
self._in_layout = True | ||
@@ -681,7 +682,8 @@ def get_sketch_params(self): | ||
""" | ||
return self._sketch | ||
def set_sketch_params(self, scale=None, length=None, randomness=None, | ||
seed=None): | ||
""" | ||
Set the sketch parameters. | ||
@@ -701,12 +703,21 @@ def set_sketch_params(self, scale=None, length=None, randomness=None): | ||
The PGF backend uses this argument as an RNG seed and not as | ||
described above. Using the same seed yields the same random shape. | ||
seed : int, optional | ||
Seed for the internal pseudo-random number generator. | ||
.. versionadded:: 3.8 | ||
AryanSheka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
.. ACCEPTS: (scale: float, length: float, randomness: float, seed: int) | ||
""" | ||
if seed is not None: | ||
self._sketch_seed = seed | ||
if scale is None: | ||
self._sketch = None | ||
else: | ||
self._sketch = (scale, length or 128.0, randomness or 16.0, | ||
self._sketch_seed) | ||
self.stale = True | ||
def set_path_effects(self, path_effects): | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -76,12 +76,13 @@ class Artist: | ||
def set_gid(self, gid: str | None) -> None: ... | ||
def get_snap(self) -> bool | None: ... | ||
def set_snap(self, snap: bool | None) -> None: ... | ||
def get_sketch_params(self) -> tuple[float, float, float, int] | None: ... | ||
AryanSheka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def set_sketch_params( | ||
self, | ||
scale: float | None = ..., | ||
length: float | None = ..., | ||
randomness: float | None = ..., | ||
seed: int | None = ..., | ||
) -> None: ... | ||
def set_path_effects(self, path_effects: list[AbstractPathEffect]) -> None: ... | ||
def get_path_effects(self) -> list[AbstractPathEffect]: ... | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -754,6 +754,19 @@ def _draw_disabled(self): | ||
return _setattr_cm(self, **no_ops) | ||
@property | ||
def _seed_increment(self): | ||
""" | ||
seed increment for renderer. | ||
It is used to implement the rolling characteristic for seed | ||
""" | ||
self.__seed_increment += 1 | ||
return self.__seed_increment | ||
@_seed_increment.setter | ||
def _seed_increment(self, value): | ||
self.__seed_increment = value | ||
class GraphicsContextBase: | ||
"""An abstract base class that provides color, line styles, etc.""" | ||
@@ -1062,7 +1075,8 @@ def get_sketch_params(self): | ||
""" | ||
return self._sketch | ||
def set_sketch_params(self, scale=None, length=None, randomness=None, | ||
seed=None): | ||
""" | ||
Set the sketch parameters. | ||
@@ -1076,10 +1090,19 @@ def set_sketch_params(self, scale=None, length=None, randomness=None): | ||
The length of the wiggle along the line, in pixels. | ||
randomness : float, default: 16 | ||
The scale factor by which the length is shrunken or expanded. | ||
seed : int, optional | ||
Seed for the internal pseudo-random number generator. | ||
AryanSheka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
.. versionadded:: 3.8 | ||
""" | ||
self._sketch = ( | ||
None if scale is None | ||
else (scale, | ||
length or rcParams['path.sketch'][1], | ||
randomness or rcParams['path.sketch'][2], | ||
seed or rcParams['path.sketch_seed']) | ||
) | ||
class TimerBase: | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -823,7 +823,8 @@ def draw(self, renderer): | ||
gc.set_foreground(ec_rgba, isRGBA=True) | ||
if self.get_sketch_params() is not None: | ||
scale, length, randomness = self.get_sketch_params() | ||
seed = self._sketch_seed | ||
gc.set_sketch_params(scale/2, length/2, 2*randomness, seed) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. since this isn't incremented, are you sure that the rolling is correctly implemented? should have lines that look different - and yes please test if you haven't | ||
marker = self._marker | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -701,8 +701,8 @@ def setp(obj, *args, **kwargs): | ||
def xkcd( | ||
scale: float = 1, length: float = 100, randomness: float = 2, | ||
seed: int | None = None) -> ExitStack: | ||
""" | ||
Turn on `xkcd <https://xkcd.com/>`_ sketch-style drawing mode. This will | ||
only have effect on things drawn after this function is called. | ||
@@ -718,6 +718,8 @@ def xkcd( | ||
The length of the wiggle along the line. | ||
randomness : float, optional | ||
The scale factor by which the length is shrunken or expanded. | ||
seed: int, optional | ||
Seed for the internal pseudo-random number generator. | ||
Notes | ||
----- | ||
@@ -738,6 +740,9 @@ def xkcd( | ||
# This cannot be implemented in terms of contextmanager() or rc_context() | ||
# because this needs to work as a non-contextmanager too. | ||
if seed is not None: | ||
rcParams['path.sketch_seed'] = seed | ||
AryanSheka marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if rcParams['text.usetex']: | ||
raise RuntimeError( | ||
"xkcd mode is not compatible with text.usetex = True") | ||
Uh oh!
There was an error while loading.Please reload this page.