code: def devectorize_axes(ax=None, dpi=None, transparent=True):
      """Convert axes contents to a png.
  
      This is useful when plotting many points, as the size of the saved file
      can become very large otherwise.
  
      Parameters
      ----------
      ax : Axes instance (optional)
          Axes to de-vectorize.  If None, this uses the current active axes
          (plt.gca())
      dpi: int (optional)
          resolution of the png image.  If not specified, the default from
          'savefig.dpi' in rcParams will be used
      transparent : bool (optional)
          if True (default) then the PNG will be made transparent
  
      Returns
      -------
      ax : Axes instance
          the in-place modified Axes instance
  
      Examples
      --------
      The code can be used in the following way::
  
      >>> import matplotlib.pyplot as plt
      >>> import numpy as np
      >>> from astroML.plotting.tools import devectorize_axes
      >>> fig, ax = plt.subplots()
      >>> x, y = np.random.random((2, 10000))
      >>> ax.scatter(x, y)   # doctest: +IGNORE_OUTPUT
      >>> devectorize_axes(ax)   # doctest: +IGNORE_OUTPUT
  
      The resulting figure will be much smaller than the vectorized version.
      """
      if ax is None:
          ax = plt.gca()
  
      fig = ax.figure
      axlim = ax.axis()
  
      # setup: make all visible spines (axes & ticks) & text invisible
      # we need to set these back later, so we save their current state
      _sp = {}
      _txt_vis = [t.get_visible() for t in ax.texts]
      for k in ax.spines:
          _sp[k] = ax.spines[k].get_visible()
          ax.spines[k].set_visible(False)
      for t in ax.texts:
          t.set_visible(False)
  
      _xax = ax.xaxis.get_visible()
      _yax = ax.yaxis.get_visible()
      _patch = ax.patch.get_visible()
      ax.patch.set_visible(False)
      ax.xaxis.set_visible(False)
      ax.yaxis.set_visible(False)
  
      # convert canvas to PNG
      extents = ax.bbox.extents / fig.dpi
      output = BytesIO()
      plt.savefig(
          output,
          format="png",
          dpi=dpi,
          transparent=transparent,
          bbox_inches=Bbox([extents[:2], extents[2:]]),
      )
      output.seek(0)
      im = image.imread(output)
  
      # clear everything on axis (but not text)
      ax.lines.clear()
      ax.patches.clear()
      ax.tables.clear()
      ax.artists.clear()
      ax.images.clear()
  
      # Show the image
      ax.imshow(
          im, extent=axlim, aspect="auto", interpolation="nearest"
      )
  
      # restore all the spines & text
      for k in ax.spines:
          ax.spines[k].set_visible(_sp[k])
      for t, v in zip(ax.texts, _txt_vis):
          t.set_visible(v)
      ax.patch.set_visible(_patch)
      ax.xaxis.set_visible(_xax)
      ax.yaxis.set_visible(_yax)
  
      if plt.isinteractive():
          plt.draw()
  
      return ax
  
file_path: astroML/astroML/plotting/tools.py
imports: import numpy
  from io import BytesIO
  from matplotlib import pyplot
  from scipy import interpolate
  from matplotlib import image
  from matplotlib.colors import LinearSegmentedColormap
  from matplotlib.transforms import Bbox
  from matplotlib.patches import Ellipse
label_desc: Use image node_id 1 for calling a global function with example usage: devectorize_axes(ax, dpi, transparent) and returns: ax