所以我节省了很多情节。
旧代码:
import matplotlib.pyplot as plt
for args in lots_of_things_to_make:
fig = plt.figure()
do_the_fancy_graphing(fig, *args)
fig.savefig(out_path)
plt.close()
我的代码的其他部分正在使用Tkinter,所以我不能使用pyplot。
新代码:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
for args in lots_of_things_to_make:
fig = Figure()
do_the_fancy_graphing(fig, *args)
canvas = FigureCanvasTkAgg(fig, master=root)
fig.savefig(out_path)
这会导致_tkinter.TclError: not enough free memory for image buffer
使用tk后端时如何关闭图?
答案 0 :(得分:1)
FigureCanvasTkAgg没有destroy方法。所以我试过了:
for args in lots_of_things_to_make:
fig = Figure()
frame = Frame(root)
do_the_fancy_graphing(fig, *args)
canvas = FigureCanvasTkAgg(fig, master=frame)
fig.savefig(out_path)
frame.destroy()
但没有运气,结果是FigureCanvasTkAgg .__ init__绑定到它所放置的顶层,所以:
for args in lots_of_things_to_make:
fig = Figure()
top = Toplevel(root)
do_the_fancy_graphing(fig, *args)
canvas = FigureCanvasTkAgg(fig, master=top)
fig.savefig(out_path)
top.destroy()
似乎对我有用。