- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
基于 John Hunter 的 answer to a question regarding using patches in animations here ,我的印象是:
True
创建一个 patch
对象.add_patch
将其添加到现有的 axis
(假设 axis
对象称为 ax
)方法。补丁
时,我会这样做:ax.draw_artist(patch)
。这样做,我遇到了以下错误:
File "environment.py", line 254, in animation_function
ax.draw_artist(patches[index])
File "A:\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 2121, in draw_artist
assert self._cachedRenderer is not None
AssertionError
顶层代码组织如下:
函数根据数据创建补丁,然后将它们添加到 axis
对象——基本上,我得到一个补丁对象列表,patches
,每个补丁都已添加到 ax
; 我认为问题可能出在这里,因为 patches
中的补丁对象不是真正连接到 ax
...,它们是添加到其中,但通过复制传递,而不是引用?
动画函数使用从 FuncAnimation
接收到的数字(比如 n
)来引用相关的补丁对象,然后调用 ax.draw_artist(补丁[n])
起初我在做以下事情:
patches = []
...
patch = mpatches.PathPatch(...)
patch.set_animated(True)
ax.add_patch(patch)
patches.append(patch)
...
ax.draw_artist(patches[n])
然后,在 reading the documentation 之后,这表明返回了一个补丁对象(可能现在连接到一个轴对象?),我尝试了以下操作:
patches = []
...
patch = mpatches.PathPatch(...)
patch.set_animated(True)
ax_patch = ax.add_patch(patch)
patches.append(ax_patch)
...
ax.draw_artist(patches[n])
但是,问题还是一样。
您能否评论一下您认为可能是什么问题,或者我可能需要在哪些方面提供额外信息才能找出问题?
编辑: 产生错误的顶级函数。
def create_animation_from_data(self, vertex_marker_radius=0.25, labels = ['a', 'a', 'b', 'b'], unit_line=0.5, colours=['red', 'blue', 'green', 'orange']):
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(-2, 100), ylim=(-2, 100))
ax.grid()
print "Initializing patches..."
patches, patch_indices_per_timestep, num_polys, num_timesteps = self.make_patches_from_environment(ax, vertex_marker_radius, labels, unit_line, colours)
def animation_function(n):
relevant_patch_indices = patch_indices_per_timestep[n]
for polygon_based_index_group in relevant_patch_indices:
for index in polygon_based_index_group:
patches[index].draw(fig.canvas.get_renderer())
return patches,
print "Beginning animation..."
ani = animation.FuncAnimation(fig, animation_function, blit=True)
plt.show()
最佳答案
您正确理解了所有内容,但缺少几个较低级别的步骤。 renderer
尚未初始化,您会收到反射(reflect)该情况的错误。
简而言之,在第一次绘制绘图之前,您不能使用 draw_artist
。您需要至少调用一次 fig.canvas.draw()
(在某些情况下您可以只调用 fig.canvas.get_renderer()
)可以使用draw_artist
。
如果您遇到这个问题,通常是因为您试图“违背常理”并做一些最好不要直接处理的事情。
你到底想做什么?可能有一种更简单的方法来处理这个问题(例如,如果您要获取背景,请将这部分代码放在绘制事件的回调中)。
让我备份并解释发生了什么。 Matplotlib Artist
使用 Renderer
实例在 FigureCanvas
(例如 fig.canvas
)上绘制(例如 fig.canvas.renderer
). renderer
是后端特定的和低级别的。您通常不会直接触摸它。
ax.draw_artist
是一个比 fig.canvas.draw
更底层的函数。更具体地说,它是 artist.draw(renderer)
的简写。
renderer
的初始化相对较慢,因此它会被缓存并重复使用,除非图形的大小或 dpi 发生变化。这就是您遇到的错误:canvas
还没有渲染器。
您有几种不同的选择。您可以手动初始化渲染器(最简单的方法是调用 fig.canvas.get_renderer()
)。然而,有时您想要获得一些在绘制之后才定义的东西(例如文本对象的大小)。在这些情况下,您需要一个“完整的”fig.canvas.draw()
。
不过,通常情况下,遇到这样的事情表明有更简单的方法可以做到这一点。通常最好将需要绘制的代码放入绘制事件的回调中。 (特别是如果它取决于图形的确切大小——例如 blitting 的背景)。
如果您使用的是 matplotlib.animation
框架,那么您不需要在更新函数中绘制艺术家。动画框架将为您处理该步骤。
听起来您似乎只想显示您在每个时间步绘制的艺术家的子集?
如果是这样,您可能会考虑切换它们的可见性。举个简单的例子:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
def main():
fig, ax = plt.subplots()
p = Plotter(ax)
# Note: We need to save a referce to the animation object, otherwise it
# will be garbage collected and never properly animate.
anim = matplotlib.animation.FuncAnimation(fig, p.update,
init_func=p.anim_init)
plt.show()
class Plotter(object):
def __init__(self, ax):
self.ax = ax
def anim_init(self):
self.triangles = [self.random_triangle() for _ in range(10)]
# Initially, show a blank plot...
for tri in self.triangles:
tri.set_visible(False)
return self.triangles
def update(self, i):
"""Animation step."""
# Hide all triangles
for tri in self.triangles:
tri.set(visible=False)
# pick 2 at random to show (e.g. your patch_indices_per_timestep)
for tri in np.random.choice(self.triangles, size=2):
tri.set(visible=True)
return self.triangles
def random_triangle(self):
x, y = np.random.random((2,3))
x += 10 * np.random.random(1)
y += 10 * np.random.random(1)
return self.ax.fill(x, y)[0]
main()
关于python - `matplotlib` : `self._chachedRenderer` fails `assert self._cachedRenderer is not None` , 在补丁上调用 `draw_artist` 时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30154473/
我正在尝试在站点列表中添加站点名称,以便在 publish:end:remote 上清除 HTML 缓存事件。 mysite 但是,它没有按预期工
我正在尝试使用不同目录中的多个文件制作补丁。我不想包含所有未提交的文件,因为我有很多不适用于补丁。 我知道这个命令可以使用特定文件创建补丁,唯一的问题是这是一个文件: svn diff -up ori
我经常使用 gnu-utils 补丁和差异。使用 git,我经常这样做: git diff 通常简单的更改会创建一个大补丁,因为唯一的更改是,例如,添加一个 if/else 循环,并且里面的所有内容都
我使用命令(从 ~ 执行)生成了一个 diff.txt 文件: diff -r /full/path/to/directory/A /full/path/to/directory/B > diff.t
我正在编写一个项目,我需要通过网络传输一组相似的图像。为了加快速度,我考虑过做大多数电影编解码器所做的事情。有关键帧,然后发送更改。 现在,我得到的是一组 BufferedImage,因此类似于文本文
我正在尝试将更改拆分为多个提交,但在手动编辑大块时我遇到了问题。 原始大块头: @@ -116,8 +116,8 @@ context context -
我想自动测试一组仍然干净利落地适用于(更新的)代码库的补丁。为此,我打算运行 patch -p 1 a echo b > b diff -Nu a b > p rm a b patch -p 1 <
我已经从一个模块中导入了一个类,但是当我尝试在没有模块作为前缀的情况下修补类名时,我得到一个类型错误: TypeError: Need a valid target to patch. You sup
最近我从 SVN 迁移到 git,我的团队已经开始在新的 git 存储库中工作。 后来我发现了一些转换问题,并从 SVN 制作了第二个 git repo,并进行了一些修复和历史重写。 现在我有了 SV
我正在尝试为我的应用程序创建一个补丁。实现描述的示例 here按预期工作。但是,每个版本的文件都存储在单独的目录中。 1.0 版文件在 c:sample\1.0 中,1.1 版文件在 c:sample
首先请原谅我的长文,但我会尽量详细。 我正在为一个开源项目 (DSpace) 进行开发。我没有对他们的 SVN 存储库的提交权限,所以我查看了源代码并一直使用 git 来管理我的版本控制。 在我的开发
有没有办法自动 merge (interdiff)2 个头分支和 1 个基分支之间的冲突? 我试图在补丁级别做这件事 版本A是我的基础 VersionB 是 VersionA 的分支 VersionB
我正在尝试申请 this修补到 MinGW 上的 GCC 以获取它 to compile GDC 2 ,但我不知道如何。 (我对 GCC 的内部结构还很陌生,甚至对一般的 *nix 工具也很陌生。)我
我对软件开发很陌生,所以这无疑是一个非常基本的问题。我得到了一个开源项目的 mercurial repo。我复制了它并做了一些工作。 promise 。工作更多,然后进行了第二次提交。所以我的树看起来
我想对这个错误 ( http://code.djangoproject.com/ticket/13095 ) 应用补丁,但我以前从未做过,也不知道从哪里开始。谁能给我指点教程? 最佳答案 在 Linu
我前段时间使用 p4 diff 生成了一个补丁。命令。 然而,现在我想应用它,我意识到没有办法在 Perforce 中应用补丁。 由于我没有使用 -du选项,补丁是那种晦涩难懂的 perforce 格
我可以使用 NSIS 的 Vpatch 生成从一个版本到另一个版本的补丁文件。假设我有 mydll.dll 版本 1,我有一个补丁可以将它更新到版本 2。然后我又有了一个新版本,因此我生成了另一个补丁
我有一个补丁,用 hg export 42 制作在另一个存储库中,修改文件 asd/fgh/foo/bar.c asd/fgh/foo/fish.h boo/hoo.txt 我需要将此补丁导入具有如下
我们有一个相当大的库,我们需要定期将其导入(然后修补)到我们的代码库中。 SVN Book 似乎推荐了一个“vendor branch”方案,我们保留了“vendor drops”的补丁版本。这会起作
我正在家里用 tensorflow 玩卷积神经网络(顺便说一句,我已经完成了 udacity 深度学习类(class),所以我有理论基础)。 运行卷积时,patch 的大小会产生什么影响?当图像变大/
我是一名优秀的程序员,十分优秀!