code: def position_nodes_for_max_tree(G, image_rav, root_x=4, delta_x=1.2):
      """Set the position of nodes of a max-tree.
  
      This function helps to visually distinguish between nodes at the same
      level of the hierarchy and nodes at different levels.
      """
      pos = {}
      for node in reversed(
          list(nx.topological_sort(canonical_max_tree))
      ):
          value = G.nodes[node]["value"]
          if canonical_max_tree.out_degree(node) == 0:
              # root
              pos[node] = (root_x, value)
  
          in_nodes = [y for y in canonical_max_tree.predecessors(node)]
  
          # place the nodes at the same level
          level_nodes = [
              y
              for y in filter(lambda x: image_rav[x] == value, in_nodes)
          ]
          nb_level_nodes = len(level_nodes) + 1
  
          c = nb_level_nodes // 2
          i = -c
          if len(level_nodes) < 3:
              hy = 0
              m = 0
          else:
              hy = 0.25
              m = hy / (c - 1)
  
          for level_node in level_nodes:
              if i == 0:
                  i += 1
              if len(level_nodes) < 3:
                  pos[level_node] = (
                      pos[node][0] + i * 0.6 * delta_x,
                      value,
                  )
              else:
                  pos[level_node] = (
                      pos[node][0] + i * 0.6 * delta_x,
                      value + m * (2 * np.abs(i) - c - 1),
                  )
              i += 1
  
          # place the nodes at different levels
          other_level_nodes = [
              y
              for y in filter(lambda x: image_rav[x] > value, in_nodes)
          ]
          if len(other_level_nodes) == 1:
              i = 0
          else:
              i = -len(other_level_nodes) // 2
          for other_level_node in other_level_nodes:
              if (len(other_level_nodes) % 2 == 0) and (i == 0):
                  i += 1
              pos[other_level_node] = (
                  pos[node][0] + i * delta_x,
                  image_rav[other_level_node],
              )
              i += 1
  
      return pos
  
file_path: scikit-image/doc/examples/developers/plot_max_tree.py
imports: import numpy
  import matplotlib.pyplot
  from matplotlib.lines import Line2D
  from skimage.morphology import max_tree
  import networkx
label_desc: Use image node_id 4 for calling a global function with example usage: position_nodes_for_max_tree(G, image_rav, root_x, delta_x) and returns: pos