Matplotlib - _tkinter.TclError:屏幕距离不好“320.0"; [英] Matplotlib - _tkinter.TclError: bad screen distance "320.0"
问题描述
File "main.py", line 52, in <module>
r2n(name)
File "C:UsersRikiDocumentsUniversitaErasmusPersonalization and Metadata modeling 02817Final Projectfriends_followers__redis_to_networkx.py", line 69, in r2n
**nx.draw_spring(g,node_size=50,node_color='#32CD32',node_shape='o',edge_color='.1',with_labels=True,width=0.5)**
File "C:Python27libsite-packages
etworkx-1.6-py2.7.egg
etworkxdrawing
x_pylab.py", line 876, in draw_spring
draw(G,spring_layout(G),**kwargs)
File "C:Python27libsite-packages
etworkx-1.6-py2.7.egg
etworkxdrawing
x_pylab.py", line 124, in draw
cf=pylab.gcf()
File "C:Python27libsite-packagesmatplotlibpyplot.py", line 369, in gcf
return figure()
File "C:Python27libsite-packagesmatplotlibpyplot.py", line 343, in figure
**kwargs)
File "C:Python27libsite-packagesmatplotlibackendsackend_tkagg.py", line 81, in new_figure_manager
canvas = FigureCanvasTkAgg(figure, master=window)
File "C:Python27libsite-packagesmatplotlibackendsackend_tkagg.py", line 188, in __init__
self._tkcanvas.create_image(w/2, h/2, image=self._tkphoto)
File "C:Python27liblib-tkTkinter.py", line 2198, in create_image
return self._create('image', args, kw)
File "C:Python27liblib-tkTkinter.py", line 2189, in _create
*(args + self._options(cnf, kw))))
**_tkinter.TclError: bad screen distance "320.0"**
您好,我正在开发适用于 Windows 64 位的 Python 2.7.突然出现这个问题,但我的代码应该没问题,因为以前可以工作(没有任何更改,图是可见的).
Hi, I'm working on Python 2.7 for Windows 64bit. Suddenly that problem occurred, but my code should be ok because was working previously (without any change the plots were visible).
我认为这是图书馆的问题,我该怎么办?
I think that is a problem with the library, what shall I do?
推荐答案
在创建画布项目之前尝试将您的坐标转换为 int.例如:
Try converting your coordinates to an int before creating the canvas item. For example:
self._tkcanvas.create_image(int(w/2), int(h/2), image=self._tkphoto)
<小时>
我非常感谢这个答案,因为它对我帮助很大;我希望我可以添加一个单独的答案,但我不能,因为它已经关闭 - 所以发布一个
I appreciate this answer very much, as it helped me a lot; I wish I could add a separate answer, but I can't as it's closed - so posting an edit:
一个对我有用且不需要更改 matplotlib
库文件的解决方案是简单地创建一个新类 覆盖一个方法,两个有问题的方法是 __init__
和 resize
(奇怪的是,所有我需要的是重载 resize
,甚至不必在那里进行修复,它就开始为我工作?)
A solution that worked for me that didn't require changing the matplotlib
library files, is to simply make a new class to override a method, the two problematic methods being __init__
and resize
(and strangely, all I need is to overload resize
, didn't even had to put in the fix there, and it started working for me?)
无论如何,请注意以下内容是从 Python2.7 Matplotlib 复制的 - 您最好先检查本地 matplotlib 版本,然后从那里复制:
Anyways, take note that the below is copied from the Python2.7 Matplotlib - you're probably better off checking for your local matplotlib version first, and copying from there:
# copy of /usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py
# with fix:
class FigureCanvasTkAggFix(FigureCanvasTkAgg):
def __init__(self, figure, master=None, resize_callback=None):
matplotlib.backends.backend_tkagg.FigureCanvasAgg.__init__(self, figure)
self._idle = True
t1,t2,w,h = self.figure.bbox.bounds
w, h = int(w), int(h)
self._tkcanvas = tk.Canvas(
master=master, width=w, height=h, borderwidth=4)
self._tkphoto = tk.PhotoImage(
master=self._tkcanvas, width=w, height=h)
self._tkcanvas.create_image(int(w/2), int(h/2), image=self._tkphoto) # fix
self._resize_callback = resize_callback
self._tkcanvas.bind("<Configure>", self.resize)
self._tkcanvas.bind("<Key>", self.key_press)
self._tkcanvas.bind("<Motion>", self.motion_notify_event)
self._tkcanvas.bind("<KeyRelease>", self.key_release)
for name in "<Button-1>", "<Button-2>", "<Button-3>":
self._tkcanvas.bind(name, self.button_press_event)
for name in "<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>":
self._tkcanvas.bind(name, self.button_release_event)
for name in "<Button-4>", "<Button-5>":
self._tkcanvas.bind(name, self.scroll_event)
root = self._tkcanvas.winfo_toplevel()
root.bind("<MouseWheel>", self.scroll_event_windows)
self._master = master
self._tkcanvas.focus_set()
self.sourced = dict()
def on_idle(*ignore):
self.idle_event()
return True
def resize(self, event):
width, height = event.width, event.height
printse("WH", width, height, "
")
if self._resize_callback is not None:
self._resize_callback(event)
# compute desired figure size in inches
dpival = self.figure.dpi
winch = width/dpival
hinch = height/dpival
self.figure.set_size_inches(winch, hinch)
self._tkcanvas.delete(self._tkphoto)
self._tkphoto = tk.PhotoImage(
master=self._tkcanvas, width=width, height=height)
self._tkcanvas.create_image(width/2,height/2,image=self._tkphoto)
self.resize_event()
self.show()
这篇关于Matplotlib - _tkinter.TclError:屏幕距离不好“320.0";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!