所以在回答这个问题的过程中,我遇到了一些来自 Tkinter 的奇怪行为。我有一个类可以调整Canvas实例的大小以及在其上绘制的任何小部件。但是,当我运行代码时,无论初始窗口尺寸如何,窗口都会不断扩展,直到填满整个屏幕。发生这种情况后,窗口的行为完全符合预期,正确调整对象的大小。该窗口仅在启动时扩展以填满屏幕。
通过阅读 Tkinter 文档,我可以相信这可能是特定于平台的(尽管我没有任何证据)。
我的问题是:为什么会发生这种情况?我怎样才能阻止它?
代码如下:
from Tkinter import *
# a subclass of Canvas for dealing with resizing of windows
class ResizingCanvas(Canvas):
def __init__(self,parent,**kwargs):
Canvas.__init__(self,parent,**kwargs)
self.bind("<Configure>", self.on_resize)
self.height = self.winfo_reqheight()
self.width = self.winfo_reqwidth()
def on_resize(self,event):
# determine the ratio of old width/height to new width/height
wscale = float(event.width)/self.width
hscale = float(event.height)/self.height
self.width = event.width
self.height = event.height
# resize the canvas
self.config(width=self.width, height=self.height)
# rescale all the objects tagged with the "all" tag …Run Code Online (Sandbox Code Playgroud) 我想我有一个大数据(N = 1e6和维度= 3)的情况.我需要在我的代码中多次执行一些矩阵操作,例如einsum,矩阵反转等.提出一个想法,我想做下面的事情.
import numpy.random as rd
ndata, kdata = 1e6, 1e5
x = rd.normal(0,1,(ndata, kdata,3,3))
y = rd.normal(0,1,(ndata, kdata,3,3))
Run Code Online (Sandbox Code Playgroud)
对于小ndata,kdata以下将是高效和方便的方法,
xy = einsum('pqrs, pqsu -> pqru', x, y )
Run Code Online (Sandbox Code Playgroud)
由于我有大的ndata和kdata以上方法成为内存绑定问题所以下一个赌注将是嵌套for循环ndata和kdata的点积,如下所示:
xyloop1 = np.empty((ndata, kdata, 3, 3))
for j in xrange(ndata):
for k in xrange(kdata):
xyloop1[j,k] = np.dot(x[j,k], y[j,k] )
Run Code Online (Sandbox Code Playgroud)
鉴于我所教授的循环在python中是令人讨厌的.此外,我想使用numpy的好处,所以思想块矩阵方法将是更好的事情如下:
nstep = 200
ndiv = ndata/nstep
kstep = 200
kdiv = kdata/kstep
xyloop2 = np.empty((ndata, kdata, 3, 3))
for j in xrange(ndiv):
ji, jf = j*nstep, (j+1)*nstep
for …Run Code Online (Sandbox Code Playgroud) 我正在开发一个交互式绘图应用程序,它要求用户从matplotlib散点图中选择数据点.为清楚起见,我希望能够在点击(或通过任何方式选择)时改变绘制点的颜色和形状.
由于matplotlib.collections.PathCollection班级有set_facecolors方法,改变点的颜色相对简单.但是,我看不到更新标记形状的类似方法.
有没有办法做到这一点?
问题的准系统说明:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(0,1.0,100)
y = np.random.normal(0,1.0,100)
scatter_plot = plt.scatter(x, y, facecolor="b", marker="o")
#update the colour
new_facecolors = ["r","g"]*50
scatter_plot.set_facecolors(new_facecolors)
#update the marker?
#new_marker = ["o","s"]*50
#scatter_plot.???(new_marker) #<--how do I access the marker shapes?
plt.show()
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我目前正在尝试在我的linux机器(Ubuntu 12.04.1 LTS)和我的新Mac(OS X 10.7.4)之间移植一些代码,当我使用python的ctypes模块访问C标准库时,我遇到了一些令人困惑的行为Mac.
为了说明问题,以下是一个最小的例子:
import ctypes as C
import numpy as np
libc = C.CDLL("/usr/lib/libc.dylib") #/usr/lib/libc.so.6 on ubuntu
np.arange(10,dtype="ubyte").tofile("test.bin") # create some test data
buffer_array = np.empty(10,dtype="ubyte") # create a reading buffer
buffer_array_c = np.ctypeslib.as_ctypes(buffer_array) # get the ctypes version of the buffer
c_file = libc.fopen("test.bin","r") # open the file through libc
libc.fread(buffer_array_c, 1, 10, c_file) # read from the file
libc.fclose(c_file)
print "Desired output:"
print np.fromfile("test.bin",dtype="ubyte")
print
print "Actual output:"
print buffer_array
Run Code Online (Sandbox Code Playgroud)
在Linux上,这可以按预期工作,产生以下内容:
Desired output:
[0 1 …Run Code Online (Sandbox Code Playgroud)