Python 如何在matplotlib中移动Poly3DCollection对象?

Python 如何在matplotlib中移动Poly3DCollection对象?,python,animation,matplotlib,Python,Animation,Matplotlib,我尝试使用matplotlib制作动画,但第一步失败。我甚至不能做一个移动的立方体。我使用plot\u surface方法创建了六个曲面,但我无法精确移动它们。我有两个问题: 如何移动Poly3DCollection对象 我尝试的方法是使用collection类提供的set\u offset。 但是这种方法只能设置x和y,而且单位很奇怪。 例如,我在(1,0,0)处创建一个立方体,并调用set_offset([100,0]), 绘制立方体的水平位置约为1.4。我尝试将此单元链接到figure对象

我尝试使用matplotlib制作动画,但第一步失败。我甚至不能做一个移动的立方体。我使用
plot\u surface
方法创建了六个曲面,但我无法精确移动它们。我有两个问题:

  • 如何移动Poly3DCollection对象

    我尝试的方法是使用collection类提供的
    set\u offset
    。 但是这种方法只能设置
    x
    y
    ,而且单位很奇怪。 例如,我在(1,0,0)处创建一个立方体,并调用set_offset([100,0]), 绘制立方体的水平位置约为1.4。我尝试将此单元链接到figure对象的dpi,但失败

  • 我的数据单位、线宽和设置偏移量的方法是什么

  • 我目前的代码如下:

        # -*- coding: utf-8 -*-
        from mpl_toolkits.mplot3d import Axes3D
        import matplotlib.pyplot as plt
    
        fig = plt.figure()
        ax = fig.gca(projection='3d')
        ax.set_aspect("equal")
        plt.xlabel('x')
        plt.ylabel('y')
    
        class cube():
    
          def __init__(self, ax, x = 0, y = 0, z = 0,
            height = 1., width = 1., depth = 1.) :
    
            self.ax = ax
            self.initiate(ax, x, y, z, height, width, depth)
    
          def initiate(self, ax, x = 0, y = 0, z = 0,
            height = 1., width = 1., depth = 1.) :
    
            self.ax = ax
            self.x, self.y, self.z = x, y, z
            self.height, self.width, self.depth = height, width, depth
    
            X = [[x, x + width], [x, x + width]]
            Y = [[y, y], [y, y]]
            Z = [[z, z], [depth, depth]]
            self.top = ax.plot_surface(X, Y, Z, linewidth=1, edgecolors='black', shade=False, color = 'red')
    
            X = [[x, x + width], [x, x + width]]
            Y = [[y + height, y + height], [y + height, y + height]]
            Z = [[z, z], [depth, depth]]
            self.bottom = ax.plot_surface(X, Y, Z, linewidth=1, edgecolors='black', shade=False, color = 'red')
    
            X = [[x, x + width], [x, x + width]]
            Y = [[y, y], [y + height, y + height]]
            Z = [[z, z], [z, z]]
            self.front = ax.plot_surface(X, Y, Z, linewidth=1, edgecolors='black', shade=False, color = 'green')
    
            X = [[x, x + width], [x, x + width]]
            Y = [[y, y], [y + height, y + height]]
            Z = [[depth, depth], [depth, depth]]
            self.back = ax.plot_surface(X, Y, Z, linewidth=1, edgecolors='black', shade=False, color = 'green')
    
            X = [[x, x], [x, x]]
            Y = [[y, y + height], [y, y + height]]
            Z = [[z, z], [depth, depth]]
            self.left = ax.plot_surface(X, Y, Z, linewidth=1, edgecolors='black', shade=False, color = 'blue')
    
            X = [[x + width, x + width], [x + width, x + width]]
            Y = [[y, y + height], [y, y + height]]
            Z = [[z, z], [depth, depth]]
            self.right = ax.plot_surface(X, Y, Z,
              linewidth=1, edgecolors='black', shade=False, color = 'blue')
    
          def set_location(self, x, y, z) :
            c.front.set_offsets([x, y])
            c.back.set_offsets([x, y])
            c.left.set_offsets([x, y])
            c.right.set_offsets([x, y])
            c.top.set_offsets([x, y])
            c.bottom.set_offsets([x, y])
    
        c = cube(ax, 1, 0, 0)
        print type(c.left)
        c.set_location(100, 0, 0)
        plt.show()
    
    根据,默认情况下以屏幕坐标应用偏移。要在数据坐标中移动对象,首先为每个对象调用
    set\u offset\u position('data')
    ,例如:

    self.top.set_offset_position('data')