I am generating saving SVG images using matplotlib
and would like to make them as reproducible as possible. However, even after setting np.random.seed
and random.seed
, the various id
and xlink:href
values in the SVG images change between runs of my code.
I assume these differences are due to the backend that matplotlib
uses to render SVG images. Is there any way to set the seed for this backend such that identical plots produce identical output between two different runs of the code?
Sample code (run this twice, changing the name in plt.savefig
for the second run):
import random
import numpy as np
import matplotlib.pyplot as plt
random.seed(42)
np.random.seed(42)
x, y = np.random.randn(4096), np.random.randn(4096)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=(64,64))
fig, axis = plt.subplots()
plt.savefig("random_1.svg")
Compare files:
diff random_1.svg random_2.svg | head
35c35
< " id="md3b71b67b7" style="stroke:#000000;stroke-width:0.8;"/>
---
> " id="m7ee1b067d8" style="stroke:#000000;stroke-width:0.8;"/>
38c38
< <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#md3b71b67b7" y="307.584"/>
---
> <use style="stroke:#000000;stroke-width:0.8;" x="57.6" xlink:href="#m7ee1b067d8" y="307.584"/>
82c82
< <use style="stroke:#000000;stroke-width:0.8;" x="129.024" xlink:href="#md3b71b67b7" y="307.584"/>
Aucun commentaire:
Enregistrer un commentaire