问题:皮查姆不显示情节

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

发生的是,一个窗口出现了不到一秒钟,然后又消失了。

在图表显示的相同代码上使用Pyzo IEP IDE(使用相同的解释程序)。

…所以问题一定出在Pycharm上。我试过使用python.exe和pythonw.exe作为解释器,两者的结果相同。

这是我的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)]'}

Pycharm does not show plot from the following code:

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.

…So the problem must be with some setting on Pycharm. I’ve tried using both python.exe and pythonw.exe as interpreter both with same results.

This is my 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)]'}

回答 0

只需使用

plt.show()

此命令告诉系统在Pycharm中绘制图。

例:

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

Just use

plt.show()

This command tells the system to draw the plot in Pycharm.

Example:

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

回答 1

我意识到这很古老,但我想我会为其他旅客消除误解。设置plt.pyplot.isinteractive()False意味着将在特定绘制命令(即plt.pyplot.show())上绘制该图。设置plt.pyplot.isinteractive()True意味着每个pyplotplt)命令将触发绘制命令(即plt.pyplot.show())。因此,您最有可能要寻找的是plt.pyplot.show()在程序结尾处显示图形。

另外,您可以使用以下import命令(import matplotlib.pyplot as plt而不是)来缩短这些语句matplotlib as plt

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 (i.e. plt.pyplot.show()). Setting plt.pyplot.isinteractive() to True means that every pyplot (plt) command will trigger a draw command (i.e. 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.

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.


回答 2

我有同样的问题。检查是否plt.isinteractive()为真。将其设置为“ False”对我有所帮助。

plt.interactive(False)

I had the same problem. Check wether plt.isinteractive() is True. Setting it to ‘False’ helped for me.

plt.interactive(False)

回答 3

我尝试了不同的解决方案,但最终对我有用的是plt.show(block=True)。您需要在命令之后添加此命令,此myDataFrame.plot()命令才能生效。如果您有多个绘图,只需在代码末尾添加命令。它将允许您查看正在绘制的每个数据。

I tried different solutions but what finally worked for me was plt.show(block=True). You need to add this command after the myDataFrame.plot() command for this to take effect. 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.


回答 4

import matplotlib
matplotlib.use('TkAgg')

为我工作。(PyCharm / OSX)

import matplotlib
matplotlib.use('TkAgg')

Works for me. (PyCharm/OSX)


回答 5

我在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 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:

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)

回答 6

我找到了解决方案。这对我有用:

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

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

回答 7

调用后不久

plt.imshow() 

呼叫

plt.show(block = True)

您将获得带有图像的matplotlib弹出窗口。

这是一种阻止方式。在弹出窗口关闭之前,其他脚本将无法运行。

Soon after calling

plt.imshow() 

call

plt.show(block = True)

You will get the matplotlib popup with the image.

This is a blocking way. Further script will not run until the pop is closed.


回答 8

对于我来说,问题是matplotlib使用了错误的后端。我正在使用Debian Jessie。

在控制台中,我执行了以下操作:

import matplotlib
matplotlib.get_backend()

结果是:“ agg”,而应该是“ TkAgg”。

解决方案很简单:

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

With me the problem was the fact that matplotlib was using the wrong backend. I am using Debian Jessie.

In a console I did the following:

import matplotlib
matplotlib.get_backend()

The result was: ‘agg’, while this should be ‘TkAgg’.

The solution was simple:

  1. Uninstall matplotlib via pip
  2. Install the appropriate libraries: sudo apt-get install tcl-dev tk-dev python-tk python3-tk
  3. Install matplotlib via pip again.

回答 9

只需添加 plt.pyplot.show(),就可以了。

最好的解决方案是禁用SciView。

Just add plt.pyplot.show(), that would be fine.

The best solution is disabling SciView.


回答 10

我在PyCharm 2017.1.2上的版本中进行了测试。我使用交互式(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)

I tested in my version on PyCharm 2017.1.2. I used interactive (True) and show (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)

回答 11

以上都不对我有用,但以下内容对我有用:

  1. 禁用pycharm中的复选框(在工具窗口中显示图)settings > Tools > Python Scientific

  2. 我收到了错误消息No PyQt5 module found。继续使用以下内容的安装PyQt5

    sudo apt-get install python3-pyqt5

请注意,仅第一步就足够了并且可以工作。

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.

  2. I received the error No PyQt5 module found. Went ahead with the installation of PyQt5 using :

    sudo apt-get install python3-pyqt5
    

Beware that for some only first step is enough and works.


回答 12

我的环境:macOS和anaconda3

这对我有用:

matplotlib.use('macosx')

或互动模式:

matplotlib.use('TkAgg')

My env: macOS & anaconda3

This works for me:

matplotlib.use('macosx')

or interactive mode:

matplotlib.use('TkAgg')

回答 13

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

i had this problem and i could solve it , you can test my way.. disable “show plots in tool window” from setting–>tools–>python scientific


回答 14

DanT的评论为我解决了这个问题,在带有GTKagg后端的linux上用pycharm修复了matplotlib。导入matplotlib时,我将得到以下错误:

>>> import matplotlib as mpl

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

当绘制这样的东西时:

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

将弹出一个图形屏幕,但没有图表出现。使用:

plt.show(block=True)

正确显示图形。

Comment from DanT fixed this for me, matplotlib with pycharm on linux with the GTKagg backend. Upon importing matplotlib I would get the following error:

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


回答 15

就我而言,我想执行以下操作:

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

在这里混合了各种解决方案之后,我的解决方案是在此之前添加以下命令:

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

以下两种进口

   import matplotlib
   import matplotlib.pyplot as plt

在我的情况下,似乎所有命令都是必需的,带有ElCapitan和PyCharm 2016.2.3的MBP。问候!

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. Greetings!


回答 16

对于初学者,您可能还需要确保正在控制台运行脚本,而不是常规Python代码。突出显示一段代码并运行它是很容易的。

For beginners, you might also want to make sure you are running your script in the console, and not as regular Python code. It is fairly easy to highlight a piece of code and run it.


回答 17

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

In non-interactive env, we have to use plt.show(block=True)


回答 18

对于那些在IDE内运行脚本(并且不能在python控制台或笔记本之类的交互式环境中工作)的人,我发现这是最直观,最简单的解决方案:

plt.imshow(img)
plt.waitforbuttonpress()

它显示了该图,并一直等到用户单击新窗口。只有这样,它才能恢复脚本并运行其余代码。

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:

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.


回答 19

我能够得到这里的一些其他建议为我工作的组合,但只有当拨动plt.interactive(False)True,然后再返回。

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

这将刷新我的情节。然后设置为False允许查看。

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

如前所述,在关闭所有窗口之前,我的程序也不会退出。以下是有关当前运行环境的一些详细信息:

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

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(True)
plt.pyplot.show()

This will flash up the my plots. Then setting to False allowed for viewing.

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

回答 20

需要为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()

One property need to set for 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()

回答 21

将导入更改为:

import matplotlib.pyplot as plt

或使用此行:

plt.pyplot.show()

Change import to:

import matplotlib.pyplot as plt

or use this line:

plt.pyplot.show()

回答 22

我正在使用Ubuntu,并且按上面的@Arie所述尝试过,但是仅在终端中使用以下行:

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

而且有效!

I’m using Ubuntu and I tried as @Arie said above but with this line only in terminal:

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

And it worked!


回答 23

在Pycharm中,有时Matplotlib.plot不会显示。

因此,调用后plt.show(),在右侧工具栏中检查SciView。在SciView内部,将存储每个生成的图。

In Pycharm , at times the Matplotlib.plot won’t show up.

So after calling plt.show() check in the right side toolbar for SciView. Inside SciView every generated plots will be stored.


回答 24

我在尝试绘制直方图时遇到上面的错误,而下面的点对我有用。

操作系统:Mac Catalina 10.15.5

Pycharm版本:社区版本2019.2.3

Python版本:3.7

  1. 我将导入语句更改如下(从-到)

来自:

import matplotlib.pylab as plt

至:

import matplotlib.pyplot as plt

  1. 和下面的绘图语句(将我的命令表pyplot更改为plt)

从:

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

至 :

plt.hist(df["horsepower"])

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

plt.show()

I was facing above error when i am trying to plot histogram and below points worked for me.

OS : Mac Catalina 10.15.5

Pycharm Version : Community version 2019.2.3

Python version : 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)

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。