【问题标题】:ipython notebook matplotlib plot for dataframe throwing non gui exceptionipython notebook matplotlib plot for dataframe throwing non gui exception
【发布时间】:2017-03-19 07:50:15
【问题描述】:

当我尝试在我的笔记本中绘制数据框时,我看到一个“非 gui”异常被抛出。

import pandas as pd
import sys,os
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%pylab inline

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
plt.figure(); df.plot();  #<-- Exception here

显示了一个绘图,绘制了df 的第一列,然后我看到了这个异常。当我尝试绘制一个系列时,我没有看到任何问题。这是异常跟踪:

(看起来我们正在尝试将 df 绘制为“交互式”图,是否有任何其他函数/参数可以尝试不“交互式”)

...\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc 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)
...\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc 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)
...\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in _plot(data, x, y, subplots, ax, kind, **kwds)
...\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in generate(self)
...\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in _make_plot(self)
...\Anaconda2\lib\site-packages\pandas\tools\plotting.pyc in _ts_plot(cls, ax, x, data, style, **kwds)
...\Anaconda2\lib\site-packages\pandas\tseries\plotting.pyc in format_dateaxis(subplot, freq)
...\Anaconda2\lib\site-packages\IPython\utils\decorators.pyc in wrapper(*args, **kw)
...\Anaconda2\lib\site-packages\matplotlib\backends\backend_tkagg.pyc in draw_if_interactive()
     68         figManager =  Gcf.get_active()
     69         if figManager is not None:
---> 70             figManager.show()
     71 
     72 class Show(ShowBase):

...\Anaconda2\lib\site-packages\matplotlib\backend_bases.pyc in show(self)
   2618         optional warning.
   2619         """
-> 2620         raise NonGuiException()
   2621 
   2622     def destroy(self):

NonGuiException: 

【问题讨论】:

  • 您的代码对我有用。这似乎是一个配置问题。您能否发布有关您的平台、您使用的 python 解释器以及所涉及的包的版本的详细信息?

标签: python python-2.7 pandas matplotlib


【解决方案1】:

解决方案

今天早上我在类似的环境(Anaconda,Python 2.7)中工作时遇到了完全相同的错误。错误消息与您的几乎相同。有趣的是,相同的代码在不同的环境、同一台机器上运行没有任何错误。所以这是第一个暗示它可能是一个包/依赖问题。

有效的解决方案是更新错误消息所引用的包。你的是:

...\Anaconda2\lib\site-packages\matplotlib\backend_bases.pyc in show(self)
   2618         optional warning.
   2619         """
-> 2620         raise NonGuiException()
   2621 
   2622     def destroy(self):

所以你会尝试更新你的matplotlib 包。既然你使用conda,我建议:

conda update matplotlib

确认 Matplotlib 已更新

您应该确认matplotlib 已更新。在您的命令提示符(终端)中,您应该看到:

cairo:       1.14.12-hab642c4_1               --> 1.14.12-hc4e6be7_4    
...
matplotlib:  2.1.0-py36h5068139_0             --> 3.0.0-py36h54f8f79_0  

或者,在您的 Jupyter Notebook / iPython 中,运行:

import matplotlib as plt
plt.__version__ # print 3.0.0

现在继续绘制 - 在 Python 2 和 3 中尝试了您的代码,并且两者都有效(再次使用 Anaconda)。

【讨论】:

    猜你喜欢
    • 2015-12-13
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 2014-02-06
    • 2015-01-08
    相关资源
    最近更新 更多