Animating bubble plots

Let's look at how to animate a bubble plot. This is useful when you want to visualize data that's transient and dynamic.

How to do it…

  1. Create a new Python file, and import the following packages:
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation 
  2. Let's define a tracker function that will dynamically update the bubble plot:
    def tracker(cur_num):
        # Get the current index 
        cur_index = cur_num % num_points
  3. Define the color:
        # Set the color of the datapoints 
        datapoints['color'][:, 3] = 1.0
  4. Update the size of the circles:
        # Update the size of the circles 
        datapoints['size'] += datapoints['growth']
  5. Update the position of the oldest datapoint in the set:
        # Update the position of the oldest datapoint 
        datapoints['position'][cur_index] = np.random.uniform(0, 1, 2)
        datapoints['size'][cur_index] = 7
        datapoints['color'][cur_index] = (0, 0, 0, 1)
        datapoints['growth'][cur_index] = np.random.uniform(40, 150)
  6. Update the parameters of the scatterplot:
        # Update the parameters of the scatter plot 
        scatter_plot.set_edgecolors(datapoints['color'])
        scatter_plot.set_sizes(datapoints['size'])
        scatter_plot.set_offsets(datapoints['position'])
  7. Define the main function and create an empty figure:
    if __name__=='__main__':
        # Create a figure 
        fig = plt.figure(figsize=(9, 7), facecolor=(0,0.9,0.9))
        ax = fig.add_axes([0, 0, 1, 1], frameon=False)
        ax.set_xlim(0, 1), ax.set_xticks([])
        ax.set_ylim(0, 1), ax.set_yticks([])
  8. Define the number of points that will be on the plot at any given point of time:
        # Create and initialize the datapoints in random positions 
        # and with random growth rates.
        num_points = 20
  9. Define the datapoints using random values:
        datapoints = np.zeros(num_points, dtype=[('position', float, 2),
                ('size', float, 1), ('growth', float, 1), ('color', float, 4)])
        datapoints['position'] = np.random.uniform(0, 1, (num_points, 2))
        datapoints['growth'] = np.random.uniform(40, 150, num_points)
  10. Create the scatterplot that will be updated every frame:
        # Construct the scatter plot that will be updated every frame
        scatter_plot = ax.scatter(datapoints['position'][:, 0], datapoints['position'][:, 1],
                          s=datapoints['size'], lw=0.7, edgecolors=datapoints['color'],
                          facecolors='none')
  11. Start the animation using the tracker function:
        # Start the animation using the 'tracker' function 
        animation = FuncAnimation(fig, tracker, interval=10)
    
        plt.show()
  12. The full code is in the dynamic_bubble_plot.py file that's already provided to you. If you run this code, you will see the following figure:
    How to do it…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.53.166