PyCharm 不显示图表并且速度很慢 [英] PyCharm doesn’t display plots and is very slow

查看:103
本文介绍了PyCharm 不显示图表并且速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次尝试 PyCharm,但在显示我的图时遇到了麻烦.你能告诉我我错过了什么吗?我在同一篇文章中对 2 个问题进行了分组,因为我觉得这实际上是同一个错误,但我不确定.我在 mac OS 10.11.6 (El Capitan) 下使用 python 3.7,不想升级.

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.

问题:使用相同的代码,在 PyCharm 中打开一个 python 控制台并使基本绘图工作但非常慢(下面的基本示例需要几秒钟,为什么?),而直接使用 PyCharm 运行它(单击绿色三角形)不显示任何内容:没有情节,没有消息.为什么?

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 ?

代码示例:

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

在 python 控制台中,几秒钟后,这给出了预期的情节.使用运行"按钮,它什么也没有.注意我复制粘贴上面 3 行之前的启动"代码,即:

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

运行"控制台只是说:

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

<小时>

下午 1:15 更新


update 1:15pm

按照从 Andrew 的评论中得出的想法,我对 matplotlib 进行了同样的尝试(与 pyqtgraph 相比,这通常是一种痛苦)并且它有效:

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()

给出了情节,但我想使用pyqtgraph(添加在标签中),通常上面的3行就足够了:不需要'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.

下午 3:20 更新

我也用 plotWidget 尝试了同样的方法,但它显示了同样的问题.以下代码:

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)

在 python 控制台中复制粘贴时给出预期的 3 线图,但运行"按钮或快捷方式(相同的运行"控制台)没有任何反应.

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).

推荐答案

看看在 pyqtgrap 示例中 将第一个示例复制并粘贴到 pycharm 中,它运行良好.(Python 3.7 与 MacOS Mojave)

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)

您需要启动 pyqt 事件循环才能看到您绘制的任何内容

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_()

将此添加到脚本的末尾应该可以解决绘图问题.关于控制台的问题,pycharm 中的控制台默认是 iPython,因此您需要运行

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  

为了使用 pyqtgraph 绘图,但在我的系统上出现错误.

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

也许您可以考虑使用 不同的 IDE,例如 spyder.我从那里测试了绘图,效果很好.

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

这篇关于PyCharm 不显示图表并且速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆