Main function. Here, the recursion into the image starts and the
    contents are picked up. The actual file gets written in this routine.

    :param figure: either a Figure object or 'gcf' (default).

    :param figurewidth: If not ``None``, this will be used as figure width
                        within the TikZ/PGFPlots output. If ``figureheight``
                        is not given, ``matplotlib2tikz`` will try to preserve
                        the original width/height ratio.
                        Note that ``figurewidth`` can be a string literal,
                        such as ``'\\figurewidth'``.
    :type figurewidth: str

    :param figureheight: If not ``None``, this will be used as figure height
                         within the TikZ/PGFPlots output. If ``figurewidth`` is
                         not given, ``matplotlib2tikz`` will try to preserve
                         the original width/height ratio.  Note that
                         ``figurewidth`` can be a string literal, such as
                         ``'\\figureheight'``.
    :type figureheight: str

    :param textsize: The text size (in pt) that the target latex document is
                     using.  Default is 10.0.
    :type textsize: float

    :param tex_relative_path_to_data: In some cases, the TikZ file will have to
                                      refer to another file, e.g., a PNG for
                                      image plots. When ``\\input`` into a
                                      regular LaTeX document, the additional
                                      file is looked for in a folder relative
                                      to the LaTeX file, not the TikZ file.
                                      This arguments optionally sets the
                                      relative path from the LaTeX file to the
                                      data.
    :type tex_relative_path_to_data: str

    :param externalize_tables: Whether or not to externalize plot data tables
                               into tsv files.
    :type externalize_tables: bool

    :param override_externals: Whether or not to override existing external
                               files (such as tsv or images) with conflicting
                               names (the alternative is to choose other
                               names).
    :type override_externals: bool

    :param strict: Whether or not to strictly stick to matplotlib's appearance.
                   This influences, for example, whether tick marks are set
                   exactly as in the matplotlib plot, or if TikZ/PGFPlots
                   can decide where to put the ticks.
    :type strict: bool

    :param wrap: Whether ``'\\begin{tikzpicture}'`` and
                 ``'\\end{tikzpicture}'`` will be written. One might need to
                 provide custom arguments to the environment (eg. scale= etc.).
                 Default is ``True``.
    :type wrap: bool

    :param add_axis_environment: Whether ``'\\begin{axis}[...]'`` and
                                 ``'\\end{axis}'`` will be written. One needs to
                                 set the environment in the document. If ``False``
                                 additionally sets ``wrap=False``. Default is ``True``.
    :type add_axis_environment: bool

    :param extra_axis_parameters: Extra axis options to be passed (as a list or set)
                                  to pgfplots. Default is ``None``.
    :type extra_axis_parameters: a list or set of strings for the pfgplots axes.

    :param extra_tikzpicture_parameters: Extra tikzpicture options to be passed
                                         (as a set) to pgfplots.

    :type extra_tikzpicture_parameters: a set of strings for the pfgplots
                                        tikzpicture.

    :param dpi: The resolution in dots per inch of the rendered image in case
                of QuadMesh plots. If ``None`` it will default to the value
                ``savefig.dpi`` from matplotlib.rcParams. Default is ``None``.
    :type dpi: int

    :param show_info: Show extra info on the command line. Default is ``False``.
    :type show_info: bool

    :param include_disclaimer: Include matplotlib2tikz disclaimer in the output.
                               Set ``False`` to make tests reproducible.
                               Default is ``True``.
    :type include_disclaimer: bool

    :param standalone: Include wrapper code for a standalone LaTeX file.
    :type standalone: bool

    :param float_format: Format for float entities. Default is ```"{:.15g}"```.
    :type float_format: str

    :param table_row_sep: Row separator for table data. Default is ```"\\n"```.
    :type table_row_sep: str

    :returns: None

    The following optional attributes of matplotlib's objects are recognized
    and handled:

     - axes.Axes._matplotlib2tikz_anchors
       This attribute can be set to a list of ((x,y), anchor_name) tuples.
       Invisible nodes at the respective location will be created which  can be
       referenced from outside the axis environment.

def get_tikz_code(
    figure="gcf",
    filepath=None,
    figurewidth=None,
    figureheight=None,
    textsize=10.0,
    tex_relative_path_to_data=None,
    externalize_tables=False,
    override_externals=False,
    strict=False,
    wrap=True,
    add_axis_environment=True,
    extra_axis_parameters=None,
    extra_tikzpicture_parameters=None,
    dpi=None,
    show_info=False,
    include_disclaimer=True,
    standalone=False,
    float_format="{:.15g}",
    table_row_sep="\n",
):
    """Main function. Here, the recursion into the image starts and the
    contents are picked up. The actual file gets written in this routine.

    :param figure: either a Figure object or 'gcf' (default).

    :param figurewidth: If not ``None``, this will be used as figure width
                        within the TikZ/PGFPlots output. If ``figureheight``
                        is not given, ``matplotlib2tikz`` will try to preserve
                        the original width/height ratio.
                        Note that ``figurewidth`` can be a string literal,
                        such as ``'\\figurewidth'``.
    :type figurewidth: str

    :param figureheight: If not ``None``, this will be used as figure height
                         within the TikZ/PGFPlots output. If ``figurewidth`` is
                         not given, ``matplotlib2tikz`` will try to preserve
                         the original width/height ratio.  Note that
                         ``figurewidth`` can be a string literal, such as
                         ``'\\figureheight'``.
    :type figureheight: str

    :param textsize: The text size (in pt) that the target latex document is
                     using.  Default is 10.0.
    :type textsize: float

    :param tex_relative_path_to_data: In some cases, the TikZ file will have to
                                      refer to another file, e.g., a PNG for
                                      image plots. When ``\\input`` into a
                                      regular LaTeX document, the additional
                                      file is looked for in a folder relative
                                      to the LaTeX file, not the TikZ file.
                                      This arguments optionally sets the
                                      relative path from the LaTeX file to the
                                      data.
    :type tex_relative_path_to_data: str

    :param externalize_tables: Whether or not to externalize plot data tables
                               into tsv files.
    :type externalize_tables: bool

    :param override_externals: Whether or not to override existing external
                               files (such as tsv or images) with conflicting
                               names (the alternative is to choose other
                               names).
    :type override_externals: bool

    :param strict: Whether or not to strictly stick to matplotlib's appearance.
                   This influences, for example, whether tick marks are set
                   exactly as in the matplotlib plot, or if TikZ/PGFPlots
                   can decide where to put the ticks.
    :type strict: bool

    :param wrap: Whether ``'\\begin{tikzpicture}'`` and
                 ``'\\end{tikzpicture}'`` will be written. One might need to
                 provide custom arguments to the environment (eg. scale= etc.).
                 Default is ``True``.
    :type wrap: bool

    :param add_axis_environment: Whether ``'\\begin{axis}[...]'`` and
                                 ``'\\end{axis}'`` will be written. One needs to
                                 set the environment in the document. If ``False``
                                 additionally sets ``wrap=False``. Default is ``True``.
    :type add_axis_environment: bool

    :param extra_axis_parameters: Extra axis options to be passed (as a list or set)
                                  to pgfplots. Default is ``None``.
    :type extra_axis_parameters: a list or set of strings for the pfgplots axes.

    :param extra_tikzpicture_parameters: Extra tikzpicture options to be passed
                                         (as a set) to pgfplots.

    :type extra_tikzpicture_parameters: a set of strings for the pfgplots
                                        tikzpicture.

    :param dpi: The resolution in dots per inch of the rendered image in case
                of QuadMesh plots. If ``None`` it will default to the value
                ``savefig.dpi`` from matplotlib.rcParams. Default is ``None``.
    :type dpi: int

    :param show_info: Show extra info on the command line. Default is ``False``.
    :type show_info: bool

    :param include_disclaimer: Include matplotlib2tikz disclaimer in the output.
                               Set ``False`` to make tests reproducible.
                               Default is ``True``.
    :type include_disclaimer: bool

    :param standalone: Include wrapper code for a standalone LaTeX file.
    :type standalone: bool

    :param float_format: Format for float entities. Default is ```"{:.15g}"```.
    :type float_format: str

    :param table_row_sep: Row separator for table data. Default is ```"\\n"```.
    :type table_row_sep: str

    :returns: None

    The following optional attributes of matplotlib's objects are recognized
    and handled:

     - axes.Axes._matplotlib2tikz_anchors
       This attribute can be set to a list of ((x,y), anchor_name) tuples.
       Invisible nodes at the respective location will be created which  can be
       referenced from outside the axis environment.
    """
    # not as default value because gcf() would be evaluated at import time
    if figure == "gcf":
        figure = plt.gcf()
    data = {}
    data["fwidth"] = figurewidth
    data["fheight"] = figureheight
    data["rel data path"] = tex_relative_path_to_data
    data["externalize tables"] = externalize_tables
    data["override externals"] = override_externals

    if filepath:
        data["output dir"] = os.path.dirname(filepath)
    else:
        directory = tempfile.mkdtemp()
        data["output dir"] = directory

    data["base name"] = (
        os.path.splitext(os.path.basename(filepath))[0] if filepath else "tmp"
    )
    data["strict"] = strict
    data["tikz libs"] = set()
    data["pgfplots libs"] = set()
    data["font size"] = textsize
    data["custom colors"] = {}
    data["legend colors"] = []
    data["extra tikzpicture parameters"] = extra_tikzpicture_parameters
    data["add axis environment"] = add_axis_environment
    data["show_info"] = show_info
    # rectangle_legends is used to keep track of which rectangles have already
    # had \addlegendimage added. There should be only one \addlegenimage per
    # bar chart data series.
    data["rectangle_legends"] = set()
    if extra_axis_parameters:
        data["extra axis options [base]"] = set(extra_axis_parameters).copy()
    else:
        data["extra axis options [base]"] = set()

    if dpi:
        data["dpi"] = dpi
    else:
        savefig_dpi = mpl.rcParams["savefig.dpi"]
        data["dpi"] = (
            savefig_dpi if isinstance(savefig_dpi, int) else mpl.rcParams["figure.dpi"]
        )

    data["float format"] = float_format
    data["table_row_sep"] = table_row_sep

    # print message about necessary pgfplot libs to command line
    if show_info:
        _print_pgfplot_libs_message(data)

    # gather the file content
    data, content = _recurse(data, figure)

    # Check if there is still an open groupplot environment. This occurs if not
    # all of the group plot slots are used.
    if "is_in_groupplot_env" in data and data["is_in_groupplot_env"]:
        content.extend("\\end{groupplot}\n\n")

    # write disclaimer to the file header
    code = """"""

    if include_disclaimer:
        disclaimer = "This file was created by matplotlib2tikz v{}.".format(__version__)
        code += _tex_comment(disclaimer)

    # write the contents
    if wrap and add_axis_environment:
        code += "\\begin{tikzpicture}\n\n"
        if extra_tikzpicture_parameters:
            code += ",\n".join(data["extra tikzpicture parameters"])
            code += "\n"

    coldefs = _get_color_definitions(data)
    if coldefs:
        code += "\n".join(coldefs)
        code += "\n\n"

    code += "".join(content)

    if wrap and add_axis_environment:
        code += "\\end{tikzpicture}"

    if standalone:
        # create a latex wrapper for the tikz
        # <https://tex.stackexchange.com/a/361070/13262>
        code = """\\documentclass{{standalone}}
\\usepackage[utf8]{{inputenc}}
\\usepackage{{pgfplots}}
\\usepgfplotslibrary{{groupplots}}
\\usepgfplotslibrary{{dateplot}}
\\pgfplotsset{{compat=newest}}
\\DeclareUnicodeCharacter{{2212}}{{−}}
\\begin{{document}}
{}
\\end{{document}}""".format(
            code
        )

    return code