[英]Making Matplotlib and GTK3 work on python3 windows
I'm trying to make GTK3 and Python3 work under windows to my project. 我正在尝试让GTK3和Python3在windows下工作到我的项目中。
I have an continuum anaconda setup with a 32-bit python 3.4 and Matplotib via conda install matplotlib. 我有一个连续的anaconda设置与32位python 3.4和Matplotib通过conda安装matplotlib。
I've installed PyGobject( https://sourceforge.net/projects/pygobjectwin32/ ) and installed GTK+ / Glade via the installer. 我安装了PyGobject( https://sourceforge.net/projects/pygobjectwin32/ )并通过安装程序安装了GTK + / Glade。
The basic exemple from the GTK3 tutorial works well (empty screen) GTK3教程的基本例子效果很好(空屏)
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
I want now to embed matplotlib in gtk, I got the example from matplotlib ( http://matplotlib.org/examples/user_interfaces/embedding_in_gtk3.html ) 我现在想在gtk中嵌入matplotlib,我从matplotlib得到了一个例子( http://matplotlib.org/examples/user_interfaces/embedding_in_gtk3.html )
I saw then that I needed cairocffi because some incompabilities. 我当时看到我需要cairocffi,因为有些不可能。 (PyCairo has no support for a matplotlib function)
(PyCairo不支持matplotlib函数)
I got the windows binaries for cffi from Gohlke 我从Gohlke获得了cffi的windows二进制文件
And finnaly did a 并且finnaly做了一个
pip install cairocffi
And now I just get a python.exe stopped working. 现在我只是让python.exe停止工作。
Tried with GTK3agg and GTK3Cairo backends and I have the same result 尝试使用GTK3agg和GTK3Cairo后端,我有相同的结果
Looking around I found that maybe the cairo version is outdated for the functions used by matplotlib, but I dont know how to proceed. 环顾四周,我发现也许cairo版本已经过时了matplotlib使用的功能,但我不知道如何继续。
Cairocffi works if I try running something else. 如果我尝试运行别的东西,Cairocffi会工作。
More information (from the comment below): 更多信息 (来自下面的评论):
I still got an unhandled win32 error. 我仍然有一个未处理的win32错误。 I managed to open the error and it says:
我设法打开错误,它说:
Unhandled exception at 0x08CF6D58 (libcairo-2.dll) in python.exe:
0xC0000005: Access violation reading location 0x000000A8.
If there is a handler for this exception, the program may be safely continued.
It just crashes... 它只是崩溃了......
I've had my share of problems using matplotlib
in Python3 + Gtk3
. 我在
Python3 + Gtk3
使用matplotlib
遇到了一些问题。 I found this cookbook page with working examples. 我发现这个食谱页面上有工作示例。 Try to run the examples in the cookbook - particularly the simplest one:
尝试在cookbook中运行示例 - 尤其是最简单的示例:
#!/usr/bin/python3
from gi.repository import Gtk
from matplotlib.figure import Figure
from numpy import arange, pi, random, linspace
import matplotlib.cm as cm
#Possibly this rendering backend is broken currently
#from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
myfirstwindow = Gtk.Window()
myfirstwindow.connect("delete-event", Gtk.main_quit)
myfirstwindow.set_default_size(400, 400)
fig = Figure(figsize=(5,5), dpi=100)
ax = fig.add_subplot(111, projection='polar')
N = 20
theta = linspace(0.0, 2 * pi, N, endpoint=False)
radii = 10 * random.rand(N)
width = pi / 4 * random.rand(N)
bars = ax.bar(theta, radii, width=width, bottom=0.0)
for r, bar in zip(radii, bars):
bar.set_facecolor(cm.jet(r / 10.))
bar.set_alpha(0.5)
ax.plot()
sw = Gtk.ScrolledWindow()
myfirstwindow.add(sw)
canvas = FigureCanvas(fig)
canvas.set_size_request(400,400)
sw.add_with_viewport(canvas)
myfirstwindow.show_all()
Gtk.main()
Also, not that you need a fairly recent version of matplotlib
to make things work on Python3
. 而且,并不是说你需要一个相当新版本的
matplotlib
才能使Python3
正常工作。
If you still have problems, please show us the complete error message. 如果您仍有问题,请告诉我们完整的错误消息。
Note: I tested this on Linux (don't have Windows), but, from you description of problems, the issue is (was) a common one. 注意:我在Linux上测试了这个(没有Windows),但是,从你的问题描述来看,这个问题是常见问题。
I have the same issue on Windows since years. 我多年来在Windows上遇到同样的问题。 The documentation of matplotlib 2.0.0 realease states that Gtk3 backend is not supported on Windows .
matplotlib 2.0.0 realease的文档声明Windows不支持Gtk3后端 。 Recently, I had an issue running the Gtk3Agg backend under Linux (Ubuntu).
最近,我遇到了在Linux(Ubuntu)下运行Gtk3Agg后端的问题。 In both cases, it is always related to Cairo.
在这两种情况下,它始终与开罗有关。
Thus, I wrote my own implementation of this backend, you can find it here . 因此,我编写了自己的后端实现,你可以在这里找到它。 import the FigureCanvasGtk3Agg from my module and use it the same way as the official one.
从我的模块导入FigureCanvasGtk3Agg并使用与官方模块相同的方式。 It lacks some features, but, if you just want to display a plot, it'll do the job.
它缺少一些功能,但是,如果你只想显示一个情节,它就会完成这项工作。
You can try the module by running it, it should display a simple colorful graph in a window. 您可以通过运行它来尝试该模块,它应该在窗口中显示一个简单的彩色图形。 I tried it under both Linux and Windows and had no issue.
我在Linux和Windows下都尝试过,没有问题。
How does it works: 它是如何工作的:
The trick is to avoid importing Cairo in Python3 as it usually doesn't work (to my experience). 诀窍是避免在Python3中导入Cairo,因为它通常不起作用(根据我的经验)。 It is done by using a
GdkPixbuf
. 它是通过使用
GdkPixbuf
完成的。
Then Gdk.cairo_set_source_pixbuf
does the rest of the job along with calling two methods of the cairo context provided by Gtk in the 'draw-event' callback. 然后
Gdk.cairo_set_source_pixbuf
完成剩下的工作,同时调用Gtk在'draw-event'回调中提供的两个cairo上下文方法。 Cairo is never imported in the module. 开罗从未在模块中导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.