尝试使用 matplotlib 内联绘图时,为啥在 jupyter notebook 中出现 NonGuiException?
Posted
技术标签:
【中文标题】尝试使用 matplotlib 内联绘图时,为啥在 jupyter notebook 中出现 NonGuiException?【英文标题】:Why do I get NonGuiException in jupyter notebook when trying to plot using matplotlib inline?尝试使用 matplotlib 内联绘图时,为什么在 jupyter notebook 中出现 NonGuiException? 【发布时间】:2017-08-18 12:30:46 【问题描述】:尝试在 jupyter notebook 中内联绘图时出现错误。
%matplotlib inline
N = 20
M = 10
df = pd.DataFrame(
index=pd.date_range(dtt.date.today(), periods=N),
data=np.random.randn(N, M),
columns=['path_'.format(ii) for ii in range(M)]
)
df.plot()
我收到以下NonGuiException
错误。即使图像绘制它似乎引发了异常。如果我使用range(N)
作为数据框的索引,它工作得很好,这很奇怪。
有什么想法吗? -------------------------------------------------- ------------------------- NonGuiException Traceback (最近一次调用最后一次) in () 6列=['path_'.format(ii) for ii in range(M)] 7) ----> 8 df.plot()
/usr/local/lib/python3.5/dist-packages/pandas/tools/plotting.py in
__call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds) 3771 fontsize=fontsize, colormap=colormap, table=table, 3772 yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 3773 sort_columns=sort_columns, **kwds) 3774 __call__.__doc__ = plot_frame.__doc__ 3775
/usr/local/lib/python3.5/dist-packages/pandas/tools/plotting.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds) 2640 yerr=yerr, xerr=xerr, 2641 secondary_y=secondary_y, sort_columns=sort_columns,
-> 2642 **kwds) 2643 2644
/usr/local/lib/python3.5/dist-packages/pandas/tools/plotting.py in
_plot(data, x, y, subplots, ax, kind, **kwds) 2467 plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds) 2468
-> 2469 plot_obj.generate() 2470 plot_obj.draw() 2471 return plot_obj.result
/usr/local/lib/python3.5/dist-packages/pandas/tools/plotting.py in generate(self) 1041 self._compute_plot_data() 1042 self._setup_subplots()
-> 1043 self._make_plot() 1044 self._add_table() 1045 self._make_legend()
/usr/local/lib/python3.5/dist-packages/pandas/tools/plotting.py in
_make_plot(self) 1724 stacking_id=stacking_id, 1725 is_errorbar=is_errorbar,
-> 1726 **kwds) 1727 self._add_legend_handle(newlines[0], label, index=i) 1728
/usr/local/lib/python3.5/dist-packages/pandas/tools/plotting.py in
_ts_plot(cls, ax, x, data, style, **kwds) 1764 lines = cls._plot(ax, data.index, data.values, style=style, **kwds) 1765
# set date formatter, locators and rescale limits
-> 1766 format_dateaxis(ax, ax.freq) 1767 return lines 1768
/usr/local/lib/python3.5/dist-packages/pandas/tseries/plotting.py in format_dateaxis(subplot, freq)
292 "t = 0 y = 1:8f".format(Period(ordinal=int(t), freq=freq), y))
293
--> 294 pylab.draw_if_interactive()
/usr/local/lib/python3.5/dist-packages/IPython/utils/decorators.py in wrapper(*args, **kw)
41 def wrapper(*args,**kw):
42 wrapper.called = False
---> 43 out = func(*args,**kw)
44 wrapper.called = True
45 return out
/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py in draw_if_interactive()
68 figManager = Gcf.get_active()
69 if figManager is not None:
---> 70 figManager.show()
71
72 class Show(ShowBase):
/usr/lib/python3/dist-packages/matplotlib/backend_bases.py in show(self) 2618 optional warning. 2619 """
-> 2620 raise NonGuiException() 2621 2622 def destroy(self):
NonGuiException:
【问题讨论】:
我卸载了 matplotlib 并重新安装它并且它工作了.. 永远是个谜......我将把它打开一两天,以防有人有一个很好的答案,否则我'将关闭 这也适用于我。感谢您提出这个问题! 【参考方案1】:命令%matplotlib inline
sets the backend。检查您没有设置冲突的后端somewhere else。
我发现我已将MPLBACKEND
环境变量设置为TkAgg
。当我从我的 pipenv
.env
文件中删除它并重新启动我的 shell 和 Jupyter 笔记本服务器后,内联绘图再次工作。
【讨论】:
以上是关于尝试使用 matplotlib 内联绘图时,为啥在 jupyter notebook 中出现 NonGuiException?的主要内容,如果未能解决你的问题,请参考以下文章
在 Google Colaboratory 上保存或下载 matplotlib 绘图图像
在 Spyder IDE 中使用 Matplotlib 绘制内联或单独的窗口