Programing

matplotlib에 플롯 창이 없습니다.

lottogame 2020. 9. 23. 08:03
반응형

matplotlib에 플롯 창이 없습니다.


시냅틱 패키지 시스템을 사용하여 Ubuntu 9.10에 matplotlib를 설치했습니다. 그러나 다음과 같은 간단한 예제를 시도하면

>>> from pylab import plot;
>>> plot([1,2,3],[1,2,3])
[<matplotlib.lines.Line2D object at 0x9aa78ec>]

플롯 창이 나타나지 않습니다. 플롯 창을 표시하는 방법에 대한 아이디어가 있습니까?


입력 할 수 있습니다.

import pylab
pylab.show()

이상을 사용하려면 ipython -pylab.


의 사용은 pylab 더 이상 권장되지 않으므로 현재 해결책은 다음과 같습니다.

import matplotlib.pyplot as plt

plt.plot([1,2,3])

plt.show()

pylab.show() 작동하지만 차단됩니다 (창을 닫아야합니다).

훨씬 더 편리한 해결책은 pylab.ion()시작할 때 수행하는 것입니다 (대화 형 모드 켜기). 모든 (pylab과 동등한 pyplot.*명령 ) 명령은 플롯을 즉시 표시합니다. 대화 형 모드에 대한 자세한 내용 은 공식 웹 사이트에서 찾을 수 있습니다.

또한 두 번째로 더 편리한 ipython -pylab( --pylab, 최신 버전)을 사용하여 from … import …부분 을 건너 뛸 수 있습니다 ( %pylab최신 IPython 버전에서도 작동 함).


이 시도:

import matplotlib
matplotlib.use('TkAgg') 

pylab을 가져 오기 전에


아래 코드 조각은 Eclipse와 Python 셸 모두에서 작동합니다.

import numpy as np
import matplotlib.pyplot as plt

# Come up with x and y
x = np.arange(0, 5, 0.1)
y = np.sin(x)

# Just print x and y for fun
print x
print y

# Plot the x and y and you are supposed to see a sine curve
plt.plot(x, y)

# Without the line below, the figure won't show
plt.show()

오류가 있습니까? 이것은 백엔드를 설정하지 않은 문제 일 수 있습니다. Python 인터프리터 또는 .matplotlib/matplotlibrc홈 디렉토리 의 구성 파일 ( )에서 설정할 수 있습니다 .

코드에서 백엔드를 설정하려면

import matplotlib
matplotlib.use('Agg')

여기서 'Agg'는 백엔드의 이름입니다. 존재하는 백엔드는 설치 및 OS에 따라 다릅니다.

http://matplotlib.sourceforge.net/faq/installing_faq.html#backends

http://matplotlib.org/users/customizing.html


Modern IPython uses the "--matplotlib" argument with an optional backend parameter. It defaults to "auto", which is usually good enough on Mac and Windows. I haven't tested it on Ubuntu or any other Linux distribution, but I would expect it to work.

ipython --matplotlib

If you encounter an issue in which pylab.show() freezes the IPython window (this may be Mac OS X specific; not sure), you can cmd-c in the IPython window, switch to the plot window, and it will break out.

Apparently, future calls to pylab.show() will not freeze the IPython window, only the first call. Unfortunately, I've found that the behavior of the plot window / interactions with show() changes every time I reinstall matplotlib, so this solution may not always hold.


If you are starting IPython with the --pylab option, you shouldn't need to call show() or draw(). Try this:

ipython  --pylab=inline

--pylab no longer works for Jupyter, but fortunately we can add a tweak in the ipython_config.py file to get both pylab as well as autoreload functionalities.

c.InteractiveShellApp.extensions = ['autoreload', 'pylab']
c.InteractiveShellApp.exec_lines = ['%autoreload 2', '%pylab']

Another possibility when using easy_install is that you need to require the most recent version of matplotlib. Try:

import pkg_resources
pkg_resources.require("matplotlib")

before you import matplotlib or any of its modules.

참고URL : https://stackoverflow.com/questions/2130913/no-plot-window-in-matplotlib

반응형