PyCharm doesn’t display plots and is very slow

谁都会走 提交于 2019-12-14 03:08:05

问题


I am trying PyCharm for the first time and have troubles to display my plots. Can you tell me what I missed ? I grouped 2 questions in the same post because I have the feeling it's in fact the same bug, but I'm not sure. I'm using python 3.7 under mac OS 10.11.6 (El Capitan) and don't want to upgrade.

Problem: with the same code, opening a python console in PyCharm and making a basic plot works but is very slow (several seconds for the basic example below, why ?), while running it directly with PyCharm (click on the green triangle) doesn’t show anything: no plot, no message. Why ?

code example :

x = np.arange(10)
y = np.arange(10)+10
pg.plot(x,y)

In the python console, after several seconds, this gives the expected plot. With the ‘run’ button it gives nothing. Note I copy-paste the ’start-up’ code before the 3 lines above, which is:

import os
import numpy as np
os.environ['PYQTGRAPH_QT_LIB'] = 'PyQt5'
import pyqtgraph as pg

The 'run' console just says:

/Users/<username>/anaconda/envs/py37/bin/python /Users/<username>/work/perso/sof/test.py
Process finished with exit code 0

update 1:15pm

Following the idea derived from Andrew's comment, I tried the same with matplotlib (which usually is a pain compared to pyqtgraph) and it works:

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = np.arange(10)+10
plt.plot(x,y)
plt.show()

gives the plot, but I would like to use pyqtgraph (added in the tags), and usually the 3 lines above are enough: no need of a 'show' command.


update 3:20pm

I also tried the same with a plotWidget but it shows the same problem. The following code:

import numpy as np
x = np.arange(10)
y = np.arange(10)+10
import os
import pyqtgraph as pg
os.environ['PYQTGRAPH_QT_LIB'] = 'PyQt5'
plotWidget = pg.plot(title='test')
plotWidget.plot(x,y)
plotWidget.plot(x,2*y)
plotWidget.plot(x,3*y)

gives the expected 3-line-plot when copy-pasted in the python console but nothing happens with the 'run' button or short-cut (same 'run' console).


回答1:


Have a look at the pyqtgrap examples Copy and paste the first example into pycharm and it works just fine. (Python 3.7 with MacOS Mojave)

You'll need to start the pyqt event loop in order to see anything you plot

if __name__ == '__main__':
import sys
if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
    pg.QtGui.QApplication.exec_()

Adding this to the end of your script should fix the plotting issue with that. Regarding the issues with the console, the console in pycharm is iPython by default so you would need to run

%gui qt5  

in order to plot with pyqtgraph, but on my system I get an error.

Perhaps you could consider using a different IDE such as spyder. I tested the plotting from there and it works very well.



来源:https://stackoverflow.com/questions/53669034/pycharm-doesn-t-display-plots-and-is-very-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!