我正在使用
我认为这些差异是由
示例代码(运行两次,更改
matplotlib
生成和保存SVG图像,并希望尽可能使它们可重现。但是,即使设置了np.random.seed
和random.seed
,SVG图像中的各种id
和xlink:href
值在运行我的代码时仍然会发生变化。我认为这些差异是由
matplotlib
用于呈现SVG图像的后端引起的。有没有办法设置此后端的种子,以便在两个不同的代码运行之间产生相同的输出,从而产生相同的图形?示例代码(运行两次,更改
plt.savefig
中的名称):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")
比较文件:
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"/>