本文介绍了在使用pyqtgraph显示图时,IPython出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python 2.7中成功安装了 pyqtgraph 库。我从GitHub分叉最新项目,然后 python setup.py install 。我现在正试图用它来展示情节。我打开一个python终端并开始键入以下内容: -

i have successfully installed pyqtgraph library in python 2.7. I forked latest project from GitHub and then python setup.py install. I am now trying to show plots with it. I open a python terminal and start typing the following:-

import pyqtgraph as pg
import numpy as np
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)

所有这些命令都已成功解释。
然后我运行命令将情节看作: -

All of these commands are successfully interpreted.But then i run the command to see the plot as:-

pg.plot(x, y, symbol='o')

输出:

<pyqtgraph.graphicsWindows.PlotWindow at 0x6b7f708>

然后打开标题为 pythonw 的窗口,说'没有回应'并挂起,我无法看到任何输出。经过长时间的窗口崩溃和终端说:

And then a windows titled pythonw opens and says 'not responding' and hangs and i am unable to see any output. After long time window crashes and terminal says:

Kernel died, restarting

可能是什么错误?我应该安装使用.exe吗?

What could be the error? Should i have installed using .exe?

编辑:
如下面titusjan所指出的,问题在于默认的Jupyter / Ipython笔记本与Anaconda一起运输我无法纠正。必须有一些安装问题。我在Windows上工作。

As pointed out below by titusjan, the problem is with the default Jupyter/Ipython notebook shipping with Anaconda which i have not been able to correct. There must be some installation problem. And i am working on Windows.

推荐答案

pyqtgraph 绘制基于的函数PyQT基于GUI的编程。因此,显示绘图的任务必须被视为启动GUI。如上所述,当我将命令提供给IPython终端时,它运行正常:

pyqtgraph plots functions based on PyQT GUI-based programming. So the task of displaying plots have to be treated as starting a GUI. As told above, when I feed the commands to the IPython terminal, it works out fine:

import numpy as np
import pyqtgraph as pg
import sys

x = np.random.normal(size=1000)
y = np.random.normal(size=1000)

如上所述,当我输入该行时问题开始:

As noted above, problem starts when i feed the line:

pg.plot(x, y, symbol='o')

要解决这个问题:要么一次输入这两行

To remedy this: either input these 2 lines in one go

pg.plot(x, y, symbol='o')
pg.QtGui.QApplication.exec_()

或紧接上一个line pg.plot(x,y,symbol ='o')输入以下行:

or immediately after the previous line pg.plot(x, y, symbol='o') input this line:

pg.QtGui.QApplication.exec_()

或者我们可以使用默认值QT-GUI命令也。因此,即使我们运行此代码,我们也会得到正确的图: -

Alternatively we could use the default QT-GUI commands also. So we get correct plots even if we run this code:-

import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import sys

x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
    QtGui.QApplication.instance().exec_()

但是,如果要避免显式调用QTGui方法,假设一个已将下面的代码保存为 xyz.py ,可以通过在命令行上写入来成功运行显示图形的代码: pythonw -i xyz的.py 。这确保已明确要求python以交互模式运行。 pythonw 用于在Windows中运行。

However, if one wants to avoid invoking QTGui methods explicitly, assuming one has saved the below code as xyz.py, one can run the code successfully displaying the graphics by writing on the command line: pythonw -i xyz.py. This ensures that python has been explicitly asked to run in interactive mode. pythonw is for running in windows.

import numpy as np
import pyqtgraph as pg

x = np.random.normal(size=1000)
y = np.random.normal(size=1000)
pg.plot(x, y, symbol='o')

这篇关于在使用pyqtgraph显示图时,IPython出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:04