{% for histogram in plot.histogram2d_list %}
    % Draw histogram background, if needed
    {%- if histogram.type in ['bw', 'reverse_bw'] %}
        {%- if histogram.type == 'bw' %}
            {%- set histogrambg = 'black' %}
        {%- elif histogram.type == 'reverse_bw' %}
            {%- set histogrambg = 'white' %}
        {%- endif %}
        {%- set x0 = histogram.x_edges[0] %}
        {%- set x1 = histogram.x_edges[-1] %}
        {%- set y0 = histogram.y_edges[0] %}
        {%- set y1 = histogram.y_edges[-1] %}
        \path[fill,{{ histogrambg }}]
            (axis cs:{{ x0 }}, {{ y0 }})
            rectangle (axis cs:{{ x1 }}, {{ y1 }});
    {%- endif %}

    % Draw non-empty bins in histogram
    {%- for i in range(histogram.x_edges|length - 1) %}
        {%- for j in range(histogram.y_edges|length - 1) %}
            {%- set x0 = histogram.x_edges[i] %}
            {%- set x1 = histogram.x_edges[i + 1] %}
            {%- set x = histogram.x_centers[i] %}
            {%- set y0 = histogram.y_edges[j] %}
            {%- set y1 = histogram.y_edges[j + 1] %}
            {%- set y = histogram.y_centers[j] %}
            {%- set count = histogram.counts[i][j] %}
            {%- set weight = (100 * count / histogram.max) | int %}

            {%- if weight > 0 %}
                {%- if histogram.type == 'area' %}
                    \draw[{{ histogram.style }},
                          scale around={ {{ count / histogram.max }}:
                                        (axis cs: {{ x }}, {{ y }})}]
                        (axis cs:{{ x0 }}, {{ y0 }})
                        rectangle (axis cs:{{ x1 }}, {{ y1 }});
                {%- elif histogram.type in ['bw', 'reverse_bw'] %}
                    \path[{{ histogram.style }},fill,
                        {%- if 'reverse' in histogram.type %}
                            {%- set weight = 100 - weight %}
                        {%- endif %}
                          black!{{ 100 - weight }}]
                        (axis cs:{{ x0 }}, {{ y0 }})
                        rectangle (axis cs:{{ x1 }}, {{ y1 }});
                {%- endif %}
            {%- endif %}
        {%- endfor %}
    {%- endfor %}
{% endfor %}

{% for bitmap in plot.bitmap_list %}
    \addplot graphics[xmin={{ bitmap.xmin }},ymin={{ bitmap.ymin }},
                      xmax={{ bitmap.xmax }},ymax={{ bitmap.ymax }}]
                     { {{ bitmap.name }}};
{% endfor %}

{%- for region in plot.shaded_regions_list %}
    \addplot[draw=none, fill={{ region.color }}] coordinates {
        {%- for x, y in region.data %}
            ({{ x }}, {{ y }})
        {%- endfor %}
            } -- cycle;
{% endfor -%}

{%- for series in plot.plot_series_list %}
    {% if series.show_xerr %}
        % Draw x-error bars
        {%- for x, y, xerr, yerr in series.data %}
            \draw (axis cs:{{ x - xerr }}, {{ y }}) --
                  (axis cs:{{ x + xerr }}, {{ y }});
        {%- endfor %}
    {%- endif %}
    {%- if series.show_yerr %}
        % Draw y-error bars
        {%- for x, y, xerr, yerr in series.data %}
            \draw (axis cs:{{ x }}, {{ y - yerr }}) --
                  (axis cs:{{ x }}, {{ y + yerr }});
        {%- endfor %}
    {% endif %}
    % Draw series plot
    \addplot[{{ series.options }}] coordinates {
        {%- for x, y, xerr, yerr in series.data %}
            ({{ x }}, {{ y }})
        {%- endfor %}
    };
{% endfor -%}

{%- for table in plot.plot_table_list %}
    {%- if limits.smin is not none and limits.smax is not none %}
        {%- set scale = (limits.smax - limits.smin) / (table.smax - table.smin) %}
    {%- else %}
        {%- set scale = 1 %}
    {%- endif %}
    % Draw table plot
    \addplot[scatter, scatter src=\thisrow{c},
             visualization depends on={\thisrow{s} \as \s},
             scatter/@pre marker code/.append style={/tikz/mark size=\s},
             {{ table.options }}]
        table {
            x y c s
        {%- for x, y, c, s in table.data %}
            {%- if limits.smin is not none and limits.smax is not none %}
                {%- set s_scaled = ((s - table.smin) * scale) + limits.smin %}
                {{ x }} {{ y }} {{ c }} {{ s_scaled }}pt
            {%- else %}
                {{ x }} {{ y }} {{ c }} {{ s }}pt
            {%- endif %}
        {%- endfor %}
        };
{% endfor -%}

{%- for line in plot.horizontal_lines %}
    \draw[{{ line.options }}]
        ({rel axis cs:0, 0} |- {axis cs:0, {{ line.value }} }) --
        ({rel axis cs:1, 1} |- {axis cs:0, {{ line.value }} });
{% endfor -%}

{%- for line in plot.vertical_lines %}
    \draw[{{ line.options }}]
        ({rel axis cs:0, 0} -| {axis cs:{{ line.value }}, 0 }) --
        ({rel axis cs:1, 1} -| {axis cs:{{ line.value }}, 0 });
{% endfor -%}

{%- for pin in plot.pin_list %}
    {%- if pin.use_arrow %}
        {%- set pin_option = 'pin' %}
    {%- else %}
        {%- set pin_option = 'label' %}
    {%- endif %}
    \node[coordinate,
          {{ pin_option }}={ [{{ pin.options }}]{{ pin.location }}:{ {{ pin.text }} }}]
        at (axis cs:{{ pin.x }}, {{ pin.y }}) {};
{% endfor -%}

{%- if plot.label %}
    \node[{{ plot.label.style }},
    {%- if plot.label.node_location != 'center' %}
          {{ plot.label.node_location }}=2pt
    {%- endif %}
        ]
        at (rel axis cs:{{ plot.label.x }},{{ plot.label.y }})
        { {{ plot.label.text }} };
{%- endif %}

{%- if plot.scalebar %}
    {%- set table = plot.plot_table_list[0] %}
    {%- set min_size_value = (table.smin * table.smin)|round|int %}
    {%- set max_size_value = (table.smax * table.smax)|round|int %}
    {%- if limits.smin is not none and limits.smax is not none %}
        {%- set scale = (limits.smax - limits.smin) / (table.smax - table.smin) %}
        {%- set smin = ((min_size_value ** 0.5 - table.smin) * scale) + limits.smin %}
        {%- set smax = ((max_size_value ** 0.5 - table.smin) * scale) + limits.smin %}
    {%- else %}
        {%- set smin = min_size_value ** 0.5 %}
        {%- set smax = max_size_value ** 0.5 %}
    {%- endif %}

    \node[
        {%- if plot.scalebar.node_location != 'center' %}
            {{ plot.scalebar.node_location }}=0pt
        {%- endif %}
        ]
        at (rel axis cs:{{ plot.scalebar.x }}, {{ plot.scalebar.y }}) {
            \begin{tikzpicture}[gray]
                {%- if smin != 0 %}
                    \draw[xshift=-{{ 1.5 * smin }}pt] (0,0) circle ({{ smin }}pt)
                        node[yshift=-{{ smin }}pt,anchor=north] { \small{ {{ min_size_value }} } };
                {%- endif %}
                \draw[xshift={{ 1.5 * smax }}pt] (0,0) circle ({{ smax }}pt)
                    node[anchor=north,yshift=-{{ smax }}pt] { \small{ {{ max_size_value }} } };
            \end{tikzpicture}
        };
{%- endif %}
