<div dir="ltr"><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41)">I'm using Python 2.7 on Windows 10 in a Jupyter Notebook.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41)">I have an external orientation sensor on a USB port. I want to periodically poll that sensor at around 10Hz and update the orientation of a figure on the screen with each new sample. The sensor returns a quaternion when polled which I convert to a rotation matrix to rotate a 3D image array. This all works fine.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41)">My problem is getting mplot3d to update the displayed image dynamically after each orientation sample. All I can get is a plot at the end of the run, nothing updates while I'm sampling. How can I change this code to plot dynamically?</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41)">Here's my code:</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:inherit;font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px;vertical-align:baseline;box-sizing:inherit;clear:both;color:rgb(36,39,41)"><i>import serial.tools.list_ports   # import serial module<br>import threespace_api_mod_Bret as ts_api<br>import numpy as np<br>import quaternion<br>import matplotlib.pyplot as plt<br>from mpl_toolkits.mplot3d import Axes3D<br>import time<br><br>def pack_quat_3space_array( array ):<br>    return np.quaternion( array[3] , array[0] , array[1] , array[2] )<br><br>class SensorAnimation( object ):<br>       <br>    def __init__(self,commport):<br>        # Create 3D axis<br>        self.fig = plt.figure()<br>        <a href="http://self.ax">self.ax</a> = self.fig.add_subplot(111, projection='3d')<br>        # Initialize the 3space sensor<br>        self.sensor = ts_api.TSUSBSensor(commport)<br>        #create the 3d image to rotate<br>        zline = np.linspace(0, 15, 1000)<br>        xline = np.sin(zline)<br>        yline = np.cos(zline)<br>        # Put the 3d data into an array for rotation<br>        self.image_array = np.array([xline,yline,zline])<br>        # Draw the initial image<br>        self.ax.plot3D(xline, yline, zline, 'red')<br>        # plt.show(block=False)<br><br>    def RefreshPosition( self , previous ):<br>        if self.sensor is not None:<br>            rot_matrix = quaternion.as_rotation_matrix(pack_quat_3space_array(sensor.getTaredOrientationAsQuaternion()))<br>            result = np.dot(rot_matrix,self.image_array)<br>            if previous is not None:<br>                try:<br>                    plt.cla()<br>                except Exception as e:<br>                    print(e)<br>            previous = self.ax.plot3D(result[0], result[1], result[2], 'green')<br>            # plt.pause(0.00001)<br>            return previous<br><br>    def Close( self ):<br>        if self.sensor is not None:<br>            self.sensor.close()<br><br><br>try:<br>    # Set up animation object<br>    sanimate = SensorAnimation("COM5")<br>    previous = None<br>    for i in range(20):<br>        previous = sanimate.RefreshPosition( previous )<br>        time.sleep(0.1)<br>    sanimate.Close()<br></i></p><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Bret Foreman<br>415-608-0604</div></div>