Python Pycharm 不显示情节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24886625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:24:46  来源:igfitidea点击:

Pycharm does not show plot

pythonmatplotlibpycharm

提问by Rasmus Larsen

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. I've tried using both python.exe and pythonw.exe as interpreter both with same results.

...所以问题一定出在 Pycharm 上的一些设置上。我试过同时使用 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)]'}

采纳答案by Amit Lohan

Just use

只需使用

plt.show()

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

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

回答by DanT

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

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

plt.interactive(False)

回答by Dhruv

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

回答by user3915686

I realize this is old but I figured I'd clear up a misconception for other travelers. Setting plt.pyplot.isinteractive()to Falsemeans that the plot will on be drawn on specific commands to draw (i.e. plt.pyplot.show()). Setting plt.pyplot.isinteractive()to Truemeans 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.

我意识到这是旧的,但我想我会澄清其他旅行者的误解。设置plt.pyplot.isinteractive()False意味着将根据要绘制的特定命令绘制绘图(即plt.pyplot.show())。设置plt.pyplot.isinteractive()True意味着每个pyplot( plt) 命令都会触发绘制命令 (即plt.pyplot.show())。因此,您很可能希望plt.pyplot.show()在程序结束时显示图形。

As a side note you can shorten these statements a bit by using the following import command import matplotlib.pyplot as pltrather than matplotlib as plt.

作为旁注,您可以使用以下导入命令import matplotlib.pyplot as plt而不是matplotlib as plt.

回答by Nathan W

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 Trueand back again.

我能够在这里将其他一些建议结合起来对我有用,但只有在再次切换plt.interactive(False)True和返回时才能使用。

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

This will flash up the my plots. Then setting to Falseallowed 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

回答by Bastiaan

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:

DanT 的评论为我解决了这个问题,matplotlib 和 pycharm 在 linux 上使用 GTKagg 后端。导入 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.

正确显示图形。

回答by Arie

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

对我来说,问题是 matplotlib 使用了错误的后端。我正在使用 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
  2. Install the appropriate libraries: sudo apt-get install tcl-dev tk-dev python-tk python3-tk
  3. Install matplotlib via pip again.
  1. 通过 pip 卸载 matplotlib
  2. 安装适当的库: sudo apt-get install tcl-dev tk-dev python-tk python3-tk
  3. 再次通过 pip 安装 matplotlib。

回答by Marc Torrellas

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!

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

回答by Charlie Armstrong

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.

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

回答by Uli Alskelung Von Hornbol

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.

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

回答by Joe Bobson

import matplotlib
matplotlib.use('TkAgg')

Works for me. (PyCharm/OSX)

为我工作。(PyCharm/OSX)