code: def _plot_and_save_attention(
      att_w, filename, xtokens=None, ytokens=None
  ):
      import matplotlib
  
      matplotlib.use("Agg")
      import matplotlib.pyplot as plt
      from matplotlib.ticker import MaxNLocator
  
      d = os.path.dirname(filename)
      if not os.path.exists(d):
          os.makedirs(d)
      w, h = plt.figaspect(1.0 / len(att_w))
      fig = plt.Figure(figsize=(w * 2, h * 2))
      axes = fig.subplots(1, len(att_w))
      if len(att_w) == 1:
          axes = [axes]
      for ax, aw in zip(axes, att_w):
          # plt.subplot(1, len(att_w), h)
          ax.imshow(aw.astype(numpy.float32), aspect="auto")
          ax.set_xlabel("Input")
          ax.set_ylabel("Output")
          ax.xaxis.set_major_locator(MaxNLocator(integer=True))
          ax.yaxis.set_major_locator(MaxNLocator(integer=True))
          # Labels for major ticks
          if xtokens is not None:
              ax.set_xticks(
                  numpy.linspace(0, len(xtokens), len(xtokens) + 1)
              )
              ax.set_xticks(
                  numpy.linspace(0, len(xtokens), 1), minor=True
              )
              ax.set_xticklabels(xtokens + [""], rotation=40)
          if ytokens is not None:
              ax.set_yticks(
                  numpy.linspace(0, len(ytokens), len(ytokens) + 1)
              )
              ax.set_yticks(
                  numpy.linspace(0, len(ytokens), 1), minor=True
              )
              ax.set_yticklabels(ytokens + [""])
      fig.tight_layout()
      return fig
  
file_path: espnet/espnet/nets/pytorch_backend/transformer/plot.py
imports: import logging
  import os
  import numpy
  from espnet.asr import asr_utils
label_desc: Use image node_id 1 for calling a global function with example usage: _plot_and_save_attention(att_w, filename, xtokens, ytokens) and returns: fig