code: def figure_to_array(fig):
      """Convert matplotlib figure to RGBA numpy array (channels-first format)."""
      buf = io.BytesIO()
      fig.savefig(buf, format="rgba")
      buf.seek(0)
      img_arr = np.reshape(
          np.frombuffer(buf.getvalue(), dtype=np.uint8),
          newshape=(
              int(fig.bbox.bounds[3]),
              int(fig.bbox.bounds[2]),
              -1,
          ),
      )
      buf.close()
      plt.close(
          fig
      )  # close here so it doesn't show up in jupyter notebook
      img_arr = img_arr.transpose(
          (2, 0, 1)
      )  # make channels first for tensorboardX
      return img_arr
  
imports: import matplotlib
  import matplotlib.pyplot
  import numpy
  import io
  from tensorboardX import SummaryWriter
label_desc: Use image node_id 3 for calling a global function with example usage: figure_to_array(fig) and returns: img_arr