简体   繁体   English

Pycharm 不显示情节

[英]Pycharm does not show plot

Pycharm does not show plot from the following code: Pycharm 不显示以下代码的绘图:

import pandas as pd
import numpy as np
import matplotlib as plt

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))

ts = ts.cumsum()    
ts.plot()

What happens is that a window appears for less than a second, and then disappears again.发生的情况是一个窗口出现不到一秒钟,然后又消失了。

Using the Pyzo IEP IDE (using same interpreter) on the same code the plot shows as expected.在相同的代码上使用 Pyzo IEP IDE(使用相同的解释器),该图按预期显示。

...So the problem must be with some setting on Pycharm. ...所以问题一定出在 Pycharm 上的一些设置上。 I've tried using both python.exe and pythonw.exe as interpreter both with same results.我试过同时使用 python.exe 和 pythonw.exe 作为解释器,结果相同。

This is my sys_info:这是我的 sys_info:

C:\pyzo2014a\pythonw.exe -u C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevconsole.py 57315 57316
PyDev console: using IPython 2.1.0import sys; print('Python %s on %s' % (sys.version, sys.platform))
Python 3.4.1 |Continuum Analytics, Inc.| (default, May 19 2014, 13:02:30) [MSC v.1600 64 bit (AMD64)] on win32
sys.path.extend(['C:\\Users\\Rasmus\\PycharmProjects\\untitled2'])
In[3]: import IPython
print(IPython.sys_info())
{'commit_hash': '681fd77',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': 'C:\\pyzo2014a\\lib\\site-packages\\IPython',
 'ipython_version': '2.1.0',
 'os_name': 'nt',
 'platform': 'Windows-8-6.2.9200',
 'sys_executable': 'C:\\pyzo2014a\\pythonw.exe',
 'sys_platform': 'win32',
 'sys_version': '3.4.1 |Continuum Analytics, Inc.| (default, May 19 2014, '
                '13:02:30) [MSC v.1600 64 bit (AMD64)]'}

Just use只需使用

import matplotlib.pyplot as plt
plt.show()

This command tells the system to draw the plot in Pycharm.该命令告诉系统在 Pycharm 中绘制绘图。

Example:例子:

plt.imshow(img.reshape((28, 28)))
plt.show()

I realize this is old but I figured I'd clear up a misconception for other travelers.我意识到这是旧的,但我想我会澄清其他旅行者的误解。 Setting plt.pyplot.isinteractive() to False means that the plot will on be drawn on specific commands to draw (ie plt.pyplot.show() ).plt.pyplot.isinteractive()设置为False意味着将根据要绘制的特定命令(即plt.pyplot.show() )绘制绘图。 Setting plt.pyplot.isinteractive() to True means that every pyplot ( plt ) command will trigger a draw command (ie plt.pyplot.show() ).plt.pyplot.isinteractive()设置为True意味着每个pyplot ( plt ) 命令都会触发绘图命令 (即plt.pyplot.show() )。 So what you were more than likely looking for is plt.pyplot.show() at the end of your program to display the graph.因此,您很可能在程序末尾寻找plt.pyplot.show()以显示图形。

As a side note you can shorten these statements a bit by using the following import command import matplotlib.pyplot as plt rather than matplotlib as plt .作为旁注,您可以通过使用以下导入命令import matplotlib.pyplot as plt而不是matplotlib as plt来稍微缩短这些语句。

I had the same problem.我有同样的问题。 Check wether plt.isinteractive() is True.检查plt.isinteractive()是否为 True。 Setting it to 'False' helped for me.将其设置为“假”对我有帮助。

plt.interactive(False)

I tried different solutions but what finally worked for me was plt.show(block=True) .我尝试了不同的解决方案,但最终对我plt.show(block=True)plt.show(block=True) You need to add this command after the myDataFrame.plot() command for this to take effect.您需要在myDataFrame.plot()命令之后添加此命令才能使其生效。 If you have multiple plot just add the command at the end of your code.如果您有多个绘图,只需在代码末尾添加命令。 It will allow you to see every data you are plotting.它将允许您查看正在绘制的每个数据。

import matplotlib
matplotlib.use('TkAgg')

Works for me.为我工作。 (PyCharm/OSX) (PyCharm/OSX)

I test in my version of Pycharm (Community Edition 2017.2.2), you may need to announce both plt.interactive(False) and plt.show(block=True) as following:我在我的 Pycharm 版本(社区版 2017.2.2)中测试,你可能需要声明 plt.interactive(False) 和 plt.show(block=True) 如下:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 6.28, 100)

plt.plot(x, x**0.5, label='square root')
plt.plot(x, np.sin(x), label='sinc')

plt.xlabel('x label')
plt.ylabel('y label')

plt.title("test plot")

plt.legend()

plt.show(block=True)
plt.interactive(False)

I have found a solution.我找到了解决办法。 This worked for me:这对我有用:

import numpy as np
import matplotlib.pyplot as plt

points = np.arange(-5, 5, 0.01)
dx, dy = np.meshgrid(points, points)
z = (np.sin(dx)+np.sin(dy))
plt.imshow(z)
plt.colorbar()
plt.title('plot for sin(x)+sin(y)')
plt.show()

Soon after calling打电话后不久

plt.imshow() 

call称呼

plt.show(block = True)

You will get the matplotlib popup with the image.您将获得带有图像的 matplotlib 弹出窗口。

This is a blocking way.这是一种阻塞方式。 Further script will not run until the pop is closed.在弹出窗口关闭之前,不会运行进一步的脚本。

With me the problem was the fact that matplotlib was using the wrong backend.对我来说,问题是 matplotlib 使用了错误的后端。 I am using Debian Jessie.我正在使用 Debian Jessie。

In a console I did the following:在控制台中,我执行了以下操作:

import matplotlib
matplotlib.get_backend()

The result was: 'agg', while this should be 'TkAgg'.结果是:'agg',而这应该是'TkAgg'。

The solution was simple:解决方法很简单:

  1. Uninstall matplotlib via pip通过 pip 卸载 matplotlib
  2. Install the appropriate libraries: sudo apt-get install tcl-dev tk-dev python-tk python3-tk安装适当的库: sudo apt-get install tcl-dev tk-dev python-tk python3-tk
  3. Install matplotlib via pip again.再次通过 pip 安装 matplotlib。

Just add plt.pyplot.show() , that would be fine.只需添加plt.pyplot.show() ,就可以了。

The best solution is disabling SciView.最好的解决方案是禁用 SciView。

None of the above worked for me but the following did:以上都不对我有用,但以下方法对我有用:

  1. Disable the checkbox (Show plots in tool window) in pycharm settings > Tools > Python Scientific .禁用 pycharm settings > Tools > Python Scientific的复选框(Show plots in tool window)。

  2. I received the error No PyQt5 module found .我收到错误No PyQt5 module found Went ahead with the installation of PyQt5 using :继续使用以下方法安装PyQt5

     sudo apt-get install python3-pyqt5

Beware that for some only first step is enough and works.请注意,对于某些人来说,仅第一步就足够了并且有效。

I tested in my version on PyCharm 2017.1.2.我在 PyCharm 2017.1.2 上的版本中进行了测试。 I used interactive (True) and show (block=True).我使用了交互式 (True) 和显示 (block=True)。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1//2000',periods=1000))
ts = ts.cumsum()
plt.interactive(True)
ts.plot()
plt.show(block=True)

For beginners, you might also want to make sure you are running your script in the console , and not as regular Python code.对于初学者,您可能还想确保在控制台运行脚本,而不是常规 Python 代码。 It is fairly easy to highlight a piece of code and run it . 突出显示一段代码并运行它是相当容易的。

My env: macOS & anaconda3我的环境:macOS 和 anaconda3

This works for me:这对我有用:

matplotlib.use('macosx')

or interactive mode:或交互模式:

matplotlib.use('TkAgg')

我有这个问题,我可以解决它,你可以测试我的方式..从设置-->工具-->python科学禁用“在工具窗口中显示图”

I was able to get a combination of some of the other suggestions here working for me, but only while toggling the plt.interactive(False) to True and back again.我能够在这里结合一些其他建议对我plt.interactive(False) ,但只有在将plt.interactive(False)切换为True并再次返回时。

plt.interactive(True)
plt.pyplot.show()

This will flash up the my plots.这将闪现我的情节。 Then setting to False allowed for viewing.然后设置为False允许查看。

plt.interactive(False)
plt.pyplot.show()

As noted also my program would not exit until all the windows were closed.如前所述,我的程序在所有窗口都关闭之前不会退出。 Here are some details on my current run environment:以下是我当前运行环境的一些详细信息:

Python version 2.7.6
Anaconda 1.9.2 (x86_64)
(default, Jan 10 2014, 11:23:15) 
[GCC 4.0.1 (Apple Inc. build 5493)]
Pandas version: 0.13.1

Comment from DanT fixed this for me, matplotlib with pycharm on linux with the GTKagg backend. DanT 的评论为我解决了这个问题,matplotlib 和 pycharm 在 linux 上使用 GTKagg 后端。 Upon importing matplotlib I would get the following error:导入 matplotlib 后,我会收到以下错误:

>>> import matplotlib as mpl

Backend GTKAgg is interactive backend. Turning interactive mode on.
Failed to enable GUI event loop integration for 'gtk'

When plotting something like so:当绘制这样的东西时:

from matplotlib import pyplot as plt
plt.figure()
plt.plot(1,2)
plt.show()

A figure screen would pop up but no charts appear.一个数字屏幕会弹出,但没有图表出现。 using:使用:

plt.show(block=True)

displays the graphic correctly.正确显示图形。

In my case, I wanted to do the following:就我而言,我想做以下事情:

    plt.bar(range(len(predictors)), scores)
    plt.xticks(range(len(predictors)), predictors, rotation='vertical')
    plt.show()

Following a mix of the solutions here, my solution was to add before that the following commands:遵循此处的混合解决方案,我的解决方案是在此之前添加以下命令:

    matplotlib.get_backend()
    plt.interactive(False)
    plt.figure()

with the following two imports有以下两个进口

   import matplotlib
   import matplotlib.pyplot as plt

It seems that all the commands are necessary in my case, with a MBP with ElCapitan and PyCharm 2016.2.3.在我的情况下,似乎所有命令都是必需的,带有 ElCapitan 和 PyCharm 2016.2.3 的 MBP。 Greetings!问候!

在非交互式环境中,我们必须使用 plt.show(block=True)

For those who are running a script inside an IDE (and not working in an interactive environment such as a python console or a notebook), I found this to be the most intuitive and the simplest solution:对于那些在 IDE 中运行脚本的人(而不是在 python 控制台或笔记本等交互式环境中工作),我发现这是最直观和最简单的解决方案:

plt.imshow(img)
plt.waitforbuttonpress()

It shows the figure and waits until the user clicks on the new window.它显示图形并等待用户单击新窗口。 Only then it resume the script and run the rest of the code.只有这样它才会恢复脚本并运行其余的代码。

One property need to set for pycharm.需要为 pycharm 设置一个属性。

import matplotlib.pyplot as plt

plt.interactive(False)  #need to set to False

dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)

plt.show()

Change import to:将导入更改为:

import matplotlib.pyplot as plt

or use this line:或使用这一行:

plt.pyplot.show()

I'm using Ubuntu and I tried as @Arie said above but with this line only in terminal:我正在使用 Ubuntu,我按照@Arie 上面所说的进行了尝试,但仅在终端中使用了这一行:

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

And it worked!它奏效了!

In Pycharm , at times the Matplotlib.plot won't show up.在 Pycharm 中,有时 Matplotlib.plot 不会出现。

So after calling plt.show() check in the right side toolbar for SciView.因此,在调用plt.show()请检查plt.show()的右侧工具栏。 Inside SciView every generated plots will be stored.在 SciView 中,每个生成的图都将被存储。

I was facing above error when i am trying to plot histogram and below points worked for me.当我尝试绘制直方图和以下点对我有用时,我遇到了上述错误。

OS : Mac Catalina 10.15.5操作系统:Mac Catalina 10.15.5

Pycharm Version : Community version 2019.2.3 Pycharm 版本:社区版 2019.2.3

Python version : 3.7 Python 版本:3.7

  1. I changed import statement as below (from - to)我将导入语句更改为如下(从 - 到)

from :从 :

import matplotlib.pylab as plt

to:到:

import matplotlib.pyplot as plt

  1. and plot statement to below (changed my command form pyplot to plt)和 plot 语句到下面(将我的命令形式 pyplot 更改为 plt)

from:从:

plt.pyplot.hist(df["horsepower"])

# set x/y labels and plot title
plt.pyplot.xlabel("horsepower")
plt.pyplot.ylabel("count")
plt.pyplot.title("horsepower bins") 

to :到 :

plt.hist(df["horsepower"])

# set x/y labels and plot title
plt.xlabel("horsepower")
plt.ylabel("count")
plt.title("horsepower bins")
  1. use plt.show to display histogram使用 plt.show 显示直方图

plt.show()

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

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