Nice programing

Pycharm이 플롯을 표시하지 않습니다.

nicepro 2020. 10. 8. 18:58
반응형

Pycharm이 플롯을 표시하지 않습니다.


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

무슨 일이 일어나고 있는지 창이 1 초 미만 동안 나타났다가 다시 사라집니다.

동일한 코드에서 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)]'}

그냥 사용

plt.show()

그것은 속임수를 쓰지만 이유를 모르겠습니다.


나는 이것이 오래되었다는 것을 알고 있지만 다른 여행자에 대한 오해를 해소 할 것이라고 생각했습니다. 로 설정 plt.pyplot.isinteractive()하면 False그릴 특정 명령 (예 :)에 플롯이 그려집니다 plt.pyplot.show(). 로 설정 plt.pyplot.isinteractive()하면 True모든 pyplot( plt) 명령이 그리기 명령 (예 :)을 트리거합니다 plt.pyplot.show(). 그래서 당신이 찾던 것보다 더 많은 것은 당신 plt.pyplot.show()의 프로그램 끝에 그래프를 표시하는 것입니다.

참고로 . import matplotlib.pyplot as plt대신 다음 가져 오기 명령을 사용하여 이러한 명령문을 약간 줄일 수 있습니다 matplotlib as plt.


나는 같은 문제가 있었다. plt.isinteractive()사실인지 확인하십시오 . 'False'로 설정하면 도움이되었습니다.

plt.interactive(False)

나는 다른 해결책을 시도했지만 마침내 나를 위해 일한 것은 plt.show(block=True). 이 명령을 적용하려면 myDataFrame.plot()명령 뒤에이 명령을 추가해야합니다 . 플롯이 여러 개인 경우 코드 끝에 명령을 추가하십시오. 플로팅하는 모든 데이터를 볼 수 있습니다.


import matplotlib
matplotlib.use('TkAgg')

나를 위해 작동합니다. (PyCharm / OSX)


해결책을 찾았습니다. 이것은 나를 위해 일했습니다.

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

내 버전의 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)

전화 후 곧

plt.imshow() 

요구

plt.show(block = True)

이미지와 함께 matplotlib 팝업이 표시됩니다.

이것은 차단 방법입니다. 팝이 닫힐 때까지 추가 스크립트가 실행되지 않습니다.


나에게 문제는 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를 다시 설치하십시오.

그냥 추가 plt.pyplot.show()하면 괜찮을 것입니다.

가장 좋은 해결책은 SciView를 비활성화하는 것입니다.


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)

내 환경 : macOS 및 anaconda3

이것은 나를 위해 작동합니다.

matplotlib.use('macosx')

또는 대화 형 모드 :

matplotlib.use('TkAgg')

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)

그래픽을 올바르게 표시합니다.


제 경우에는 다음을 수행하고 싶었습니다.

    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!


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.


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


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

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
    

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

Change import to:

import matplotlib.pyplot as plt

or use this line:

plt.pyplot.show()

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.


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!

참고URL : https://stackoverflow.com/questions/24886625/pycharm-does-not-show-plot

반응형