How to move a Poly3DCollection object in matplotlib?












1















I try to make an animation using matplotlib and fail at the first step. I cannot even make a moving cube. I created six surface by using the plot_surface method but I cant move them precisely. I have two questions:





  1. How to move a Poly3DCollection object.



    The method I tried is using set_offsets provided by the collection class.
    But this method can only set x and y and the unit is weird.
    For example, I create a cube at (1, 0 ,0) and invoke set_offsets([100, 0]),
    the horizontal position of the plotted cube is approximately 1.4. I try to link this unit to the dpi of the figure object but failed.



  2. What are the units for my data, the line width, and the set_offsets method.



My current code is as follows:



    # -*- 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()









share|improve this question



























    1















    I try to make an animation using matplotlib and fail at the first step. I cannot even make a moving cube. I created six surface by using the plot_surface method but I cant move them precisely. I have two questions:





    1. How to move a Poly3DCollection object.



      The method I tried is using set_offsets provided by the collection class.
      But this method can only set x and y and the unit is weird.
      For example, I create a cube at (1, 0 ,0) and invoke set_offsets([100, 0]),
      the horizontal position of the plotted cube is approximately 1.4. I try to link this unit to the dpi of the figure object but failed.



    2. What are the units for my data, the line width, and the set_offsets method.



    My current code is as follows:



        # -*- 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()









    share|improve this question

























      1












      1








      1








      I try to make an animation using matplotlib and fail at the first step. I cannot even make a moving cube. I created six surface by using the plot_surface method but I cant move them precisely. I have two questions:





      1. How to move a Poly3DCollection object.



        The method I tried is using set_offsets provided by the collection class.
        But this method can only set x and y and the unit is weird.
        For example, I create a cube at (1, 0 ,0) and invoke set_offsets([100, 0]),
        the horizontal position of the plotted cube is approximately 1.4. I try to link this unit to the dpi of the figure object but failed.



      2. What are the units for my data, the line width, and the set_offsets method.



      My current code is as follows:



          # -*- 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()









      share|improve this question














      I try to make an animation using matplotlib and fail at the first step. I cannot even make a moving cube. I created six surface by using the plot_surface method but I cant move them precisely. I have two questions:





      1. How to move a Poly3DCollection object.



        The method I tried is using set_offsets provided by the collection class.
        But this method can only set x and y and the unit is weird.
        For example, I create a cube at (1, 0 ,0) and invoke set_offsets([100, 0]),
        the horizontal position of the plotted cube is approximately 1.4. I try to link this unit to the dpi of the figure object but failed.



      2. What are the units for my data, the line width, and the set_offsets method.



      My current code is as follows:



          # -*- 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()






      python animation matplotlib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 4 '15 at 21:38









      Shuo LiShuo Li

      9616




      9616
























          1 Answer
          1






          active

          oldest

          votes


















          0














          According to the PolyCollection documentation, the offsets are applied in screen coordinates by default. To move your objects in data coordinates, first call set_offset_position('data') for each of your objects, e.g.



          self.top.set_offset_position('data')





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29451671%2fhow-to-move-a-poly3dcollection-object-in-matplotlib%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            According to the PolyCollection documentation, the offsets are applied in screen coordinates by default. To move your objects in data coordinates, first call set_offset_position('data') for each of your objects, e.g.



            self.top.set_offset_position('data')





            share|improve this answer




























              0














              According to the PolyCollection documentation, the offsets are applied in screen coordinates by default. To move your objects in data coordinates, first call set_offset_position('data') for each of your objects, e.g.



              self.top.set_offset_position('data')





              share|improve this answer


























                0












                0








                0







                According to the PolyCollection documentation, the offsets are applied in screen coordinates by default. To move your objects in data coordinates, first call set_offset_position('data') for each of your objects, e.g.



                self.top.set_offset_position('data')





                share|improve this answer













                According to the PolyCollection documentation, the offsets are applied in screen coordinates by default. To move your objects in data coordinates, first call set_offset_position('data') for each of your objects, e.g.



                self.top.set_offset_position('data')






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 15:26









                Paul DaumPaul Daum

                1




                1
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29451671%2fhow-to-move-a-poly3dcollection-object-in-matplotlib%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)