code: def ensure_matplotlib_figure(obj):
      
      import matplotlib
      from matplotlib.figure import Figure
      if obj == matplotlib.pyplot:
          obj = obj.gcf()
      elif not isinstance(obj, Figure):
          if hasattr(obj, "figure"):
              obj = obj.figure
              # Some matplotlib objects have a figure function
              if not isinstance(obj, Figure):
                  raise ValueError(
                      "Only matplotlib.pyplot or matplotlib.pyplot.Figure objects are accepted.")
      if not obj.gca().has_data():
          raise ValueError(
              "You attempted to log an empty plot, pass a figure directly or ensure the global plot isn't closed.")
      return obj
docs: Extract the current figure from a matplotlib object or return the object if it's a figure.
      raises ValueError if the object can't be converted.