如何腌制很多子图?

How to pickle a lot of subplots?

是否可以用 18*18 的子图腌制图形?

figure = plt.figure()
axes = figure.subplots(18, 18, sharex='all', sharey='all', gridspec_kw={'hspace': 0, 'wspace': 0})
for ax in axes.flat:
    ax.set_yticks([])
    ax.set_xticks([])
with open('fig.pickle', 'wb') as file:
    pickle.dump(figure, file)

当我尝试这样做时,出现错误:递归错误:调用 Python 对象时超出最大递归深度

这可以通过 sys 模块增加系统递归限制来解决。递归限制是为了提前停止任何 unoptimized/buggy 递归,并且对于这个用例可以安全地增加到 ~10,000(前提是你有 RAM 来处理它)。

这是一个工作示例:

import pickle 
import sys

import matplotlib.pyplot as plt 


sys.setrecursionlimit(10_000) #this makes it work


figure = plt.figure()
axes = figure.subplots(18, 18, sharex='all', sharey='all', gridspec_kw={'hspace': 0, 'wspace': 0})
for ax in axes.flat:
    ax.set_yticks([])
    ax.set_xticks([])

with open('fig.pickle', 'wb+') as outfile:
    pickle.dump(figure, outfile)