我正在研究这个 matplotlib 示例,但不理解点语法:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
class DraggablePoint:
lock = None #only one can be animated at a time
def __init__(self, point):
self.point = point
self.press = None
self.background = None
def connect(self):
'connect to all the events we need'
self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.cidrelease = self.point.figure.canvas.mpl_connect('button_release_event', self.on_release)
self.cidmotion = self.point.figure.canvas.mpl_connect('motion_notify_event', self.on_motion)
def on_press(self, event):
if event.inaxes != self.point.axes: return
if DraggablePoint.lock is not None: return
contains, attrd = self.point.contains(event)
if not contains: return
self.press = (self.point.center), event.xdata, event.ydata
DraggablePoint.lock = self
# draw everything but the selected rectangle and store the pixel buffer
canvas = self.point.figure.canvas
axes = self.point.axes
self.point.set_animated(True)
canvas.draw()
self.background = canvas.copy_from_bbox(self.point.axes.bbox)
# now redraw just the rectangle
axes.draw_artist(self.point)
# and blit just the redrawn area
canvas.blit(axes.bbox)
def on_motion(self, event):
if DraggablePoint.lock is not self:
return
if event.inaxes != self.point.axes: return
self.point.center, xpress, ypress = self.press
dx = event.xdata - xpress
dy = event.ydata - ypress
self.point.center = (self.point.center[0]+dx, self.point.center[1]+dy)
canvas = self.point.figure.canvas
axes = self.point.axes
# restore the background region
canvas.restore_region(self.background)
# redraw just the current rectangle
axes.draw_artist(self.point)
# blit just the redrawn area
canvas.blit(axes.bbox)
def on_release(self, event):
'on release we reset the press data'
if DraggablePoint.lock is not self:
return
self.press = None
DraggablePoint.lock = None
# turn off the rect animation property and reset the background
self.point.set_animated(False)
self.background = None
# redraw the full figure
self.point.figure.canvas.draw()
def disconnect(self):
'disconnect all the stored connection ids'
self.point.figure.canvas.mpl_disconnect(self.cidpress)
self.point.figure.canvas.mpl_disconnect(self.cidrelease)
self.point.figure.canvas.mpl_disconnect(self.cidmotion)
fig = plt.figure()
ax = fig.add_subplot(111)
drs = []
circles = [patches.Circle((0.32, 0.3), 0.03, fc='r', alpha=0.5),
patches.Circle((0.3,0.3), 0.03, fc='g', alpha=0.5)]
for circ in circles:
ax.add_patch(circ)
dr = DraggablePoint(circ)
dr.connect()
drs.append(dr)
plt.show()
Run Code Online (Sandbox Code Playgroud)
现在以该行为例
ax.add_patch(circ)
这对我来说似乎很清楚。该类axes有一个名为的方法add_patch,该方法将(特别是)Circle对象作为参数。因此只需从作为实例的ax.add_patch(circ)对象调用此方法即可。axaxes
里面的点import matplotlib.patches似乎有不同的含义。它只是访问模块,patches该模块是模块列表的子模块,matplotlib请参阅http://matplotlib.org/1.3.1/py-modindex.html。
据我所知,模块只是一个包含一些类和函数的 Python 文件。
现在考虑:
self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.on_press)
self.point是pointinit 中定义的变量(不需要是固定类型)。稍后在代码中,有通过where is a objectDraggablePoint实例化的对象。现在我很难解释。在这种情况下,不能是函数,因为末尾没有。dr = DraggablePoint(circ)circpatches.Circleself.point.figurefigure()
对我来说,在这种情况下将其视为模块也是没有意义的。我猜这是一种简写形式,类似于self.point.get_current_figure()返回绘制点的图形。
类似地,这self.point.figure.canvas似乎是self.point.get_current_figure().get_canvas()返回当前画布的东西。然而,类中似乎没有get_current_figureorget_canvas方法。mathplotlib.patches.Circ类mathmatplotlib.figure.Figure(参见:
http://matplotlib.org/1.3.1/api/artist_api.html#module-matplotlib.patches和http://matplotlib.org/1.3.1/api/figure_api.html#matplotlib.figure 。数字)。
因此,如果有人能为我澄清这一点,那就太好了。更一般地说:
Python 中的点符号似乎有多种不同的含义。它们有哪些,它们是如何称呼的以及我如何知道使用哪一个?
我如何才能看到我可以调用self.point.figure或self.point.figure.canvas从 matplotlib API 文档中调用?如上所述,我在文档中没有找到它。
只是.访问一个属性。属性可以是类、实例、方法/函数等。当您看到类似 的内容时a.b.c,它指的是 属性 的属性,c其中、、和可以是上面提到的任何类型。换句话说,它是的属性。baabcca.b
()末尾没有的事实并不意味着该属性不是函数。考虑以下:
>>> class Foo:
... def __init__(self):
... import os
... self.number = 1
... self.module = os
... self.class_ = Exception
... self.function = dir
...
>>> f = Foo()
Run Code Online (Sandbox Code Playgroud)
模块可以是一个属性:
>>> f.module
<module 'os' from '/usr/lib/python2.7/os.pyc'>
>>> f.module.path.join('foo', 'bar')
'foo/bar'
Run Code Online (Sandbox Code Playgroud)
类可以是一个属性:
>>> f.class_
<type 'exceptions.Exception'>
>>> raise f.class_('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: foo
Run Code Online (Sandbox Code Playgroud)
函数可以是属性:
>>> f.function
<built-in function dir>
>>> f.function('.')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Run Code Online (Sandbox Code Playgroud)
如果你想知道是否可以调用某些东西,请使用以下callable函数:
>>> callable(f.module)
False
>>> callable(f.function)
True
Run Code Online (Sandbox Code Playgroud)
如果您想了解属性是什么或如何使用它,请首先使用该help函数读取其文档字符串。例如:
help(f.function)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5309 次 |
| 最近记录: |