code: def set_legend_position(
      legend: AnchoredOffsetbox,
      position: LegendPosition,
      tparams: TightParams,
      fig: Figure,
  ):
      """
      Place legend and align it centerally with respect to the panels
      """
      grid = tparams.grid
      spaces = tparams.spaces
  
      if position in ("right", "left"):
          y = (grid.top + grid.bottom) / 2
          if position == "left":
              x = spaces.l.edge("legend")
              loc = "center left"
          else:
              x = spaces.r.edge("legend")
              loc = "center right"
      elif position in ("top", "bottom"):
          x = (grid.right + grid.left) / 2
          if position == "top":
              y = spaces.t.edge("legend")
              loc = "upper center"
          else:
              y = spaces.b.edge("legend")
              loc = "lower center"
      else:
          x, y = position
          loc = "center"
  
      anchor_point = (x, y)
      legend.loc = AnchoredOffsetbox.codes[loc]
      legend.set_bbox_to_anchor(anchor_point, fig.transFigure)
  
file_path: plotnine/plotnine/_mpl/_plotnine_tight_layout.py
imports: from __future__ import annotations
  import typing
  from copy import deepcopy
  from dataclasses import dataclass
  from matplotlib.offsetbox import AnchoredOffsetbox
  from ._plot_side_space import LRTBSpaces, WHSpaceParts, calculate_panel_spacing
label_desc: Use image node_id 5 for calling a global function with example usage: set_legend_position(legend, position, tparams, fig) without return types