docstr: Move bubbles to the center of mass.
  
  Parameters
  ----------
  n_iterations : int, default: 50
      Number of moves to perform.
docstr_tok: ["Move","bubbles","to","the","center","of","mass",".","Parameters","--","--","--","--","--","n_iterations",":","int",",","default",":","50","Number","of","moves","to","perform","."]
code: def collapse(self, n_iterations=50):
      """
      Move bubbles to the center of mass.
  
      Parameters
      ----------
      n_iterations : int, default: 50
          Number of moves to perform.
      """
      for _i in range(n_iterations):
          moves = 0
          for i in range(len(self.bubbles)):
              rest_bub = np.delete(self.bubbles, i, 0)
              # try to move directly towards the center of mass
              # direction vector from bubble to the center of mass
              dir_vec = self.com - self.bubbles[i, :2]
  
              # shorten direction vector to have length of 1
              dir_vec = dir_vec / np.sqrt(dir_vec.dot(dir_vec))
  
              # calculate new bubble position
              new_point = self.bubbles[i, :2] + dir_vec * self.step_dist
              new_bubble = np.append(new_point, self.bubbles[i, 2:4])
  
              # check whether new bubble collides with other bubbles
              if not self.check_collisions(new_bubble, rest_bub):
                  self.bubbles[i, :] = new_bubble
                  self.com = self.center_of_mass()
                  moves += 1
              else:
                  # try to move around a bubble that you collide with
                  # find colliding bubble
                  for colliding in self.collides_with(
                      new_bubble, rest_bub
                  ):
                      # calculate direction vector
                      dir_vec = (
                          rest_bub[colliding, :2] - self.bubbles[i, :2]
                      )
                      dir_vec = dir_vec / np.sqrt(dir_vec.dot(dir_vec))
                      # calculate orthogonal vector
                      orth = np.array([dir_vec[1], -dir_vec[0]])
                      # test which direction to go
                      new_point1 = (
                          self.bubbles[i, :2] + orth * self.step_dist
                      )
                      new_point2 = (
                          self.bubbles[i, :2] - orth * self.step_dist
                      )
                      dist1 = self.center_distance(
                          self.com, np.array([new_point1])
                      )
                      dist2 = self.center_distance(
                          self.com, np.array([new_point2])
                      )
                      new_point = (
                          new_point1 if dist1 < dist2 else new_point2
                      )
                      new_bubble = np.append(
                          new_point, self.bubbles[i, 2:4]
                      )
                      if not self.check_collisions(
                          new_bubble, rest_bub
                      ):
                          self.bubbles[i, :] = new_bubble
                          self.com = self.center_of_mass()
  
          if moves / len(self.bubbles) < 0.1:
              self.step_dist = self.step_dist / 2
  
file_path: matplotlib/galleries/examples/misc/packed_bubbles.py
imports: import matplotlib.pyplot
  import numpy
label_desc: Use image node_id 7 for calling the BubbleChart obj's underlying member method code with example usage: obj.collapse(n_iterations) without return types