Programing

파이썬의 matplotlib에서 '백엔드'를 어떻게 설정할 수 있습니까?

lottogame 2020. 10. 27. 07:48
반응형

파이썬의 matplotlib에서 '백엔드'를 어떻게 설정할 수 있습니까?


저는 matplotlib의 새로운 사용자이며 내 플랫폼은 Ubuntu 10.04 Python 2.6.5입니다.

이것은 내 코드입니다

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt 
plt.plot([1,2,3])

오류는 다음과 같습니다.

/usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py:41: UserWarning: 
Your currently selected backend, 'agg' does not support show().
Please select a GUI backend in your matplotlibrc file ('/usr/local/lib/python2.6/dist-packages/matplotlib/mpl-data/matplotlibrc')
or with matplotlib.use()
  (backend, matplotlib.matplotlib_fname()))
  • Anti-Grain Geometry 라이브러리를 설치 apt-get install libagg했지만 작동하지 않습니다.
  • 'GTK'와 'TkAgg'와 같은 백엔드의 다른 인수를 사용해 보았습니다.
  • python-gtk2-dev패키지를 설치 했지만 여전히 오류는 다음과 같습니다.
  • 누구든지 실행 가능한 백엔드 인수와 해당 종속성 라이브러리를 말해 줄 수 있습니까?

다음은 오류입니다.

>>> matplotlib.use('GTK')
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/pyplot.py", line 95, in <module>
    new_figure_manager, draw_if_interactive, show = pylab_setup()
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/__init__.py", line 25, in pylab_setup
    globals(),locals(),[backend_name])
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_gtk.py", line 28, in <module>
    from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK 
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/backends/backend_gdk.py", line 29, in <module>
    from matplotlib.backends._backend_gdk import pixbuf_get_pixels_array
ImportError: No module named _backend_gdk

현재 선택한 백엔드 'agg'는 show ()를 지원하지 않습니다.

AGG백엔드는 창에서 렌더링하는 것이 아니라 파일에 쓰기위한 것입니다. matplotlib 웹 사이트에서 백엔드 FAQ참조하십시오 .

ImportError : _backend_gdk라는 모듈이 없습니다.

두 번째 오류의 경우 matplotlib 배포가 GTK 지원으로 컴파일되지 않았거나 PyGTK 패키지가 누락되었을 수 있습니다. 그것을 설치하십시오.

show()그래픽 환경에 액세스 할 수있는 터미널 또는 응용 프로그램 내부 에서 메서드 를 호출 합니까?

다음 순서로 다른 GUI 백엔드를 시도하십시오.

  • TkAgg
  • WX
  • QTAgg
  • QT4Agg

참고로, matplotlib.use('Agg')Python 가져 오기 순서를 먼저 입력해야한다는 것을 알았습니다 . 내가하고있는 일 (단위 테스트는 헤드리스가 필요함)을 위해

import matplotlib
matplotlib.use('Agg')

내 마스터 테스트 스크립트 상단에 있습니다. 다른 파일을 만질 필요가 없었습니다.


matplotlibrc예를 들어 다음과 같이 구성 파일 (오류 메시지에 설명 된대로) 에서 설정할 수도 있습니다 .

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
backend : Agg

이렇게하면 코드가 다른 사람과 공유되는 경우 백엔드를 하드 코딩 할 필요가 없습니다. 자세한 내용은 설명서를 확인하십시오 .


The errors you posted are unrelated. The first one is due to you selecting a backend that is not meant for interactive use, i.e. agg. You can still use (and should use) those for the generation of plots in scripts that don't require user interaction.

If you want an interactive lab-environment, as in Matlab/Pylab, you'd obviously import a backend supporting gui usage, such as Qt4Agg (needs Qt and AGG), GTKAgg (GTK an AGG) or WXAgg (wxWidgets and Agg).

I'd start by trying to use WXAgg, apart from that it really depends on how you installed Python and matplotlib (source, package etc.)


I hit this when trying to compile python, numpy, scipy, matplotlib in my own VIRTUAL_ENV

Before installing matplotlib you have to build and install: pygobject pycairo pygtk

And then do it with matplotlib: Before building matplotlib check with 'python ./setup.py build --help' if 'gtkagg' backend is enabled. Then build and install

Before export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig


Before starting python, you can do in bash

export MPLBACKEND=TkAgg

참고URL : https://stackoverflow.com/questions/4930524/how-can-i-set-the-backend-in-matplotlib-in-python

반응형