Django matplotlib tkinter.TclError:NULL主窗口

时间:2018-08-19 20:43:09

标签: django matplotlib tkinter pdflatex

我一直在使用matplotlib.pyplot,pdflatex和django来创建一个应用程序,该应用程序从数据库中检索数据,根据该数据创建png图表,并编译包含该图像的tex文件。我创建了四种图表,每种图表都与一个函数相关联:

#A bar/line with a rezised chart
plt.figure(figsize=(16.0, 10.0))
plt.bar(y_pos, values)
plt.plot(areas, standardValues)
plt.clf()

# A barchart with resized chart
plt.figure(figsize=(16.0, 10.0))
plt.bar(y_pos, diferrenceValues)
plt.clf()

# 10 histogram with distribution chart
shape, loc, scale = stats.lognorm.fit(hours_per_program, floc=0)
x_fit = np.linspace(data.min(), data.max(), 100)
hours_per_program_fit = stats.lognorm.pdf(x_fit, shape, loc=loc, scale=scale)
ax.hist(data, N_bins)
plt.clf()

# And 10 side bar charts
plt.barh(r1, data1, barHeight)
plt.barh(r2, data2, barHeight)
plt.barh(r3, data3, barHeight)
plt.clf()

每种图表的功能均按该顺序和该计时器的数量被调用。所以当我第一次生成pdf

...
File "/home/me/docs/project/t1/client/views.py", line 1359, in generatePdf
lineBarChart(areas_names, areas_program_hours, areas_standard_value, "cliente/static/img")
File "/home/me/docs/project/t1/client/chartGenerator.py", line 45, in lineBarChart
plt.figure(figsize=(16.0, 10.0))
File "/home/me/docs/project/t1/venv/lib/python3.5/site-packages/matplotlib/pyplot.py", line 548, in figure
**kwargs)
File "/home/me/docs/project/t1/venv/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 161, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File "/home/me/docs/project/t1/venv/lib/python3.5/site-packages/matplotlib/backends/_backend_tk.py", line 1053, in new_figure_manager_given_figure
icon_img = Tk.PhotoImage(file=icon_fname)
File "/usr/lib/python3.5/tkinter/__init__.py", line 3397, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.5/tkinter/__init__.py", line 3353, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: NULL main window

所以tkinter,我想是matplotlib使用它来生成图像的方法抛出了这个异常,即没有主窗口,我真的不明白为什么会这样,我生成了22个图表没有问题,但是当时需要再次调用函数,我无法调整大小,也无法生成图表,因为没有tkinter窗口。
我真的很感谢您的帮助,我已经为这个问题奋斗了几天,现在我找不到解决方案。 提前谢谢

0 个答案:

没有答案