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')
It outputs:
<pyqtgraph.graphicsWindows.PlotWindow at 0x6b7f708>
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
What could be the error? Should i have installed using .exe?
EDIT:
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 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_()
or immediately after the previous line pg.plot(x, y, symbol='o') input this line:
pg.QtGui.QApplication.exec_()
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_()
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')
Related
I am currently using Python 2.7 on Ubuntu 16.04 and would like the ability to plot figures using Matplotlib. However, calling any sort of plot command causes the entire script to hang at that line.
** Note ** Before marking this as a duplicate question, please consider that this issue may not be related to the backend that I am using as I have tried every iteration of this solution that I have found on the internet.
A simple script which illustrates my problem:
#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
plt.interactive('True')
if __name__ == '__main__':
print 'Hello World!'
plt.figure()
print 'Hello Again World!'
The output of the above script is: Hello World!
The script then hangs at the plt.figure() line, causes 100% cpu usage, and cannot be killed using Ctrl-C. I use "kill" to kill that process.
I never figured out what caused my problem, but my (poor) solution was to reinstall Ubuntu 16.04 and then matplotlib. It is working fine now.
Thanks everyone!
I am using Pyinstaller (after spending a long time with py2exe) to convert my REAL.py file to .exe. I used Anaconda to make .py file which is running perfectly on my computer. But when I make .exe file, it shows no error and an application is created in dist\REAL folder. But when I run the .exe file, the console opens and closes instantly.
It should ideally show a GUI window and take inputs and use them to make plots. It does so when I run REAL.py file. I am using Tkinter, Matplotlib, numpy, scipy which comes with Anaconda.
EDIT: I tried to run simple code to check the compatibility with matplotlib:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The same issue persists with this. Opens console window and then closes but no plot is given out.
I found the solution in py2exe. Following was the setup.py file that worked with Tkinter Matplotlib numpy scipy imports:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
opts = {"py2exe": {
"packages" : ['matplotlib'],
"includes": ['scipy', 'scipy.integrate', 'scipy.special.*','scipy.linalg.*'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll']
}
}
setup(
windows = [{'script': "with_GUI.py"}], zipfile = None,
options= opts,
data_files = matplotlib.get_py2exe_datafiles()
)
But this gave me some error saying that there was version conflict with two files. So I changed the two files viz. dist/tcl/tcl8.5/init.tcl (in line 19) and dist/tcl/tk8.5/tk.tcl (in line 18). In my case I changed the version from 8.5.15 to 8.5.18. I found the location of the two files by looking at the path specified by the error in error log. Then the .exe worked just fine.
I hope this helps.
Try using the --hidden-import=matplotlib when calling pyinstaller. For example, in the command prompt you would type:
Pyinstaller --hidden-import=matplotlib your_filename_here.py
and you could try giving it a shot with tkinter in there as well.
Pyinstaller --hidden-import=matplotlib --hidden-import=tkinter your_filename_here.py
I am trying to make a plot using matplotlib in ipython notebook.
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
I get the desired plot, but when I try to close it, it hangs. I then have to force quit it, and restart the kernel.
If I run the same code from terminal, I can safely close the plot. The error is when using ipython notebook.
I am on OS X El Capitan (10.11.2), python 2.7. The issue began after updating to El Capitan.
Thanks in advance.
When your work in a notebook, it is better to display the plots inline. Turn on the inline mode:
In: [1] %matplotlib inline
Now, plot without the plt.show():
In: [2]import matplotlib.pyplot as plt
In: [3]plt.plot(range(10))
The plot should appear right in the notebook.
For interactive plots in the notebook use mpld3. Just add these lines to your notebook:
import mpld3
mpld3.enable_notebook()
I setup Anaconda 2.0.0 (Win 64).
It has SymPy 0.7.5.
I configured Spyder (2.3.0rc that came with Anaconda) to use symbolic math:
Tools > Preferences > iPython console > Advanced Settings > Symbolic Mathematics
I create a new project and a new file:
# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
integrate(x, x)
print("Completed.")
When I run this (Python or iPython console) it does not print the integral -- it only prints Completed.
But what is weird is that while in the console that just did the run if I then re-type:
integrate(x, x)
It does print the integral.
So running from a file never prints any symbolic math but typing in the console manually does?
Can anyone help with this issue -- maybe it some sort of configuration?
Thank you!
Running a script is not the same as executing code in IPython. When you run the code in a cell or prompt in IPython, it captures the output of the last command and displays it to you. When you run a script, the script is just run, and the only thing that is displayed is what is printed to the screen.
I don't think there is a way to send the IPython display object (which would be needed to get pretty latex output) from a script, but I may be misunderstanding how spyder executes the code in IPython, or missing some hooks that it has. You can try
from IPython.display import display
display(integrate(x, x))
It is because integrate doesn't print automatically, it just returns the output. You will have to pass it to print function to get the output. Try using following code:
# -*- coding: utf-8 -*-
from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
print(integrate(x, x))
print("Completed.")
In Python console(or IPython console) returned statements are automatically printed.
Update: Use pprint for a nice formatted output.
Im completely new to pygame. after installing it and looking at a tutorial, the first code block i ran (from the tutorial) took me to the pygame modlule, and said that pygame.base could not be found.
it couldn't find any of these at all:
from pygame.base import *
from pygame.constants import *
from pygame.version import *
from pygame.rect import Rect
from pygame.compat import geterror
import pygame.rwobject
import pygame.surflock
import pygame.color
that's from the pygame module.
I looked in both the pygame files that were installed and saw files with those names, though i could not open them (unknown file type)
i installed the python 2.7 binary for windows version of pygame, and used the installer. everything looks like it's in the right place as far as i can tell.
It doesn't seem that you have pygame properly installed. Can you even successfully run import pygame?
Are you sure you only have one copy of python installed?