简体   繁体   English

matplotlib 绘图窗口不会出现

[英]matplotlib plot window won't appear

I'm using Python 2.7.3 in 64-bit.我在 64 位中使用 Python 2.7.3。 I installed pandas as well as matplotlib 1.1.1, both for 64-bit.我为 64 位安装了 Pandas 和 matplotlib 1.1.1。 Right now, none of my plots are showing.现在,我的图都没有显示。 After attempting to plot from several different dataframes, I gave up in frustration and tried the following first example from http://pandas.pydata.org/pandas-docs/dev/visualization.html :在尝试从几个不同的数据帧绘图后,我沮丧地放弃并尝试了http://pandas.pydata.org/pandas-docs/dev/visualization.html 中的以下第一个示例:

INPUT:输入:

import matplotlib.pyplot as plt
ts = Series(randn(1000), index=date_range ('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()
pylab.show()

OUTPUT:输出:

Axes(0.125,0.1;0.775x0.8)

And no plot window appeared.并且没有出现绘图窗口。 Other StackOverflow threads I've read suggested I might be missing DLLs.我读过的其他 StackOverflow 线程表明我可能缺少 DLL。 Any suggestions?有什么建议么?

I'm not convinced this is a pandas issue at all.我根本不相信这是熊猫问题。

Does

import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()

bring up a plot?提出一个情节?

If not:如果不:

How did you install matplotlib?你是如何安装 matplotlib 的? Was it from source or did you install it from a package manager/pre-built binary?它是来自源代码还是您是从包管理器/预构建的二进制文件中安装的?

I suspect that if you run:我怀疑如果你运行:

import matplotlib            
print matplotlib.rcParams['backend']

The result will be a non-GUI backend (almost certainly "Agg").结果将是一个非 GUI 后端(几乎可以肯定是“Agg”)。 This suggests you don't have a suitable GUI toolkit available (I personally use Tkinter which means my backend is reported as "TkAgg").这表明您没有合适的 GUI 工具包可用(我个人使用 Tkinter,这意味着我的后端报告为“TkAgg”)。

The solution to this depends on your operating system, but if you can install a GUI library (one of Tkinter, GTK, QT4, PySide, Wx) then pyplot.show() should hopefully pop up a window for you.解决方案取决于您的操作系统,但如果您可以安装 GUI 库(Tkinter、GTK、QT4、PySide、Wx 之一),那么pyplot.show()应该会为您弹出一个窗口。

HTH,哈,

I had this problem when working from within a virtualenv.在 virtualenv 中工作时遇到了这个问题。

Cause原因

The cause of the problem is that when you pip install matplotlib , it fails to find any backends (even if they are installed on your machine), so it uses the "agg" backend, which does not make any plots, just writes files.问题的原因是,当您pip install matplotlib ,它无法找到任何后端(即使它们已安装在您的机器上),因此它使用了“agg”后端,该后端不进行任何绘图,仅写入文件。 To confirm that this is the case, go: python -c "import matplotlib; print matplotlib.get_backend()" , you probably see agg .要确认是这种情况,请执行: python -c "import matplotlib; print matplotlib.get_backend()" ,您可能会看到agg

I could however, successfully use matplotlib on the system (outside the virtualenv).但是,我可以在系统上成功使用 matplotlib(在 virtualenv 之外)。 I also failed to install PySide, PyQt, or get it to work for TkAgg, for various different reasons.由于各种不同的原因,我也未能安装 PySide、PyQt 或让它为 TkAgg 工作。

Solution解决方案

I eventually just made a link to my system version of matplotlib (starting from outside the venv):我最终只是建立了一个指向我的系统版本的 matplotlib 的链接(从 venv 外部开始):

...$ pip install matplotlib
...$ cd /to/my/venv/directory
...$ source venv/bin/activate
(venv) .... $ pip uninstall matplotlib
(venv) .... $ ln -s /usr/lib/pymodules/python2.7/matplotlib $VIRTUAL_ENV/lib.python*/site-packages

After that, I could use matplotlib and the plots would show.之后,我可以使用 matplotlib 并显示图表。 Your local version of matplotlib may be in a different place.您本地版本的 matplotlib 可能位于不同的位置。 To see where it is, go (outside of the venv, in python)要查看它在哪里,请转到(在 venv 之外,在 python 中)

...$ python -c 'import matplotlib; matplotlib.__file__'

尝试安装这些库,它对我有用:

$ sudo apt-get install tcl-dev tk-dev python-tk python3-tk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM