我目前正在尝试构建一个包含一些pylab
数字的简单应用程序。我刚开始使用quickly
,我担心一些导入问题。也就是说,所有matplotlib
后端都无法工作。
import
更具体地说,如果我的主文件中以下任何语句被取消注释,quickly
则将无法运行该应用程序
#from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
#from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
#from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas
所以我没有办法嵌入情节。
quickly run
如果backend_gtk
取消注释,则输出如下
/usr/lib/python2.7/dist-packages/gobject/constants.py:24: Warning: g_boxed_type_register_static: assertion `g_type_from_name (name) == 0' failed import gobject._gobject
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40: Warning: specified class size for type `PyGtkGenericCellRenderer' is smaller than the parent type's `GtkCellRenderer' class size from gtk import
_gtk
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40: Warning: g_type_get_qdata: assertion `node != NULL' failed from gtk import _gtk
有什么办法可以解决这个问题吗?您需要更多信息吗?
答案1
您至少需要 1.2.0 版本的 matplotlib,然后使用 GTK3 后端。
示例取自http://matplotlib.org/examples/user_interfaces/embedding_in_gtk3.html
#!/usr/bin/env python
"""
demonstrate adding a FigureCanvasGTK3Agg widget to a Gtk.ScrolledWindow
using GTK3 accessed via pygobject
"""
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import arange, sin, pi
from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit )
win.set_default_size(400,300)
win.set_title("Embedding in GTK")
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
t = arange(0.0,3.0,0.01)
s = sin(2*pi*t)
a.plot(t,s)
sw = Gtk.ScrolledWindow()
win.add (sw)
# A scrolled window border goes outside the scrollbars and viewport
sw.set_border_width (10)
canvas = FigureCanvas(f) # a Gtk.DrawingArea
canvas.set_size_request(800,600)
sw.add_with_viewport (canvas)
win.show_all()
Gtk.main()
注意:Ubuntu 12.04 存储库中没有 Matplotlib 1.2.0,但可以通过以下方式安装pip
:
sudo pip install matplotlib