将PatchCollection添加到matplotlib Axes3D时,Tkinter回调中出现TypeError

将PatchCollection添加到matplotlib Axes3D时,Tkinter回调中出现TypeError,matplotlib,tkinter,Matplotlib,Tkinter,以下代码改编自多边形示例 它对PolyCollection很有效,但当我切换到PatchCollection时,我开始出现以下错误: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__ return self.func(*args) File "/usr/l

以下代码改编自多边形示例

它对PolyCollection很有效,但当我切换到PatchCollection时,我开始出现以下错误:

Exception in Tkinter callback Traceback (most recent call last):   
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
        return self.func(*args)   
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 590, in callit
        func(*args)   
File "/home/csquires/.local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 370, in idle_draw
        self.draw()   
File "/home/csquires/.local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw
        FigureCanvasAgg.draw(self)   
File "/home/csquires/.local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 464, in draw
        self.figure.draw(self.renderer)   
File "/home/csquires/.local/lib/python2.7/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
        draw(artist, renderer, *args, **kwargs)   
File "/home/csquires/.local/lib/python2.7/site-packages/matplotlib/figure.py", line 1144, in draw
        renderer, self, dsu, self.suppressComposite)   
File "/home/csquires/.local/lib/python2.7/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
        a.draw(renderer)   
File "/home/csquires/.local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 271, in draw
        for col in self.collections]   
File "/home/csquires/.local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/art3d.py", line 387, in do_3d_projection
        vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)   
File "/home/csquires/.local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/proj3d.py", line 207, in proj_transform_clip
        return proj_transform_vec_clip(vec, M)   
File "/home/csquires/.local/lib/python2.7/site-packages/mpl_toolkits/mplot3d/proj3d.py", line 164, in proj_transform_vec_clip
        vecw = np.dot(M, vec) TypeError: can't multiply

sequence by non-int of type 'float'

看起来这与后端有关-有什么建议可以替代吗?

您需要使用Patch3DCollection而不是PatchCollection

使用以下内容导入Patch3dCollection:

from mpl_toolkits.mplot3d.art3d import Patch3DCollection
并将PatchCollection更改为Patch3dCollection,即

patch_collection = Patch3DCollection(patch_list, match_original=True)

正如我在给Jannick的评论中提到的,使用Patch3DCollection仍然会给我带来问题,没有将补丁正确地附加到图形上。相反,我需要单独创建每个面片,将其添加到轴,然后将其更改为三维:

zs = range(4)
colors = ['r', 'g', 'b', 'y']
for i, z in enumerate(zs):
    rect = patches.Rectangle(xy=(0, 0), width=10, height=10, color=cc(colors[i]))
    ax.add_patch(rect)
    art3d.patch_2d_to_3d(rect, z=z, zdir='y')

这使得面片被绘制出来,但现在我得到了一个浮动矩形,它没有附加到任何轴上(即,当我旋转绘图时,它保持在同一位置)
zs = range(4)
colors = ['r', 'g', 'b', 'y']
for i, z in enumerate(zs):
    rect = patches.Rectangle(xy=(0, 0), width=10, height=10, color=cc(colors[i]))
    ax.add_patch(rect)
    art3d.patch_2d_to_3d(rect, z=z, zdir='y')