简体   繁体   English

Matplotlib:自定义Qt4Agg后端

[英]Matplotlib: Customizing Qt4Agg Backend

I am trying to customize the Qt4Agg backend. 我正在尝试自定义Qt4Agg后端。 To ensure that I didn't insert any bugs, as a starting point, I copied matplotlib/backends/backend_qt4agg.py to a new location on my PYTHONPATH, renamed it "my_backend.py", and corrected two import statements as follows: 为了确保没有插入任何错误,作为起点,我将matplotlib / backends / backend_qt4agg.py复制到了我的PYTHONPATH上的新位置,并将其重命名为“ my_backend.py”,并更正了以下两个导入语句:

from backend_agg import ...
from backend_qt4 import ...

becomes

from matplotlib.backends.backend_agg import ...
from matplotlib.backends.backend_qt4 import ...

In my matplotlib RC file I changed the backend to module://my_backend.py. 在我的matplotlib RC文件中,我将后端更改为module://my_backend.py。

When plotting from a script using "show()", everything works as normal. 当使用“ show()”从脚本进行绘制时,一切正常。 But when trying to execute within an ipython session started with "ipython --pylab", the figure shows up, but nothing ever plots and I get a spinning wheel. 但是,当尝试在以“ ipython --pylab”开头的ipython会话中执行时,该图出现了,但是没有任何显示,我得到了一个旋转的轮子。 How can I fix this? 我怎样才能解决这个问题?

I am using OSX Mavericks, Python 2.7.3, IPython 1.1.0, and Matplotlib 1.3.1. 我正在使用OSX Mavericks,Python 2.7.3,IPython 1.1.0和Matplotlib 1.3.1。

Thanks 谢谢

The problem occurs because the GUI main loop fails to start because IPython does not recognize the backend, and therefore, it doesn't know which GUI to use. 发生问题是因为GUI主循环无法启动,因为IPython无法识别后端,因此它不知道要使用哪个GUI。 This can be remedied by manually starting the GUI main loop in an IPython startup file. 可以通过在IPython启动文件中手动启动GUI主循环来解决此问题。 In ~/.ipython/profile_default/startup/my_backend_start_gui.py I placed the following code: 在〜/ .ipython / profile_default / startup / my_backend_start_gui.py中,我放置了以下代码:

import matplotlib
if matplotlib.rcParams['backend'] == 'module://my_backend':
    from IPython.lib.inputhook import enable_gui
    enable_gui('qt4')

Now everything works fine. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM