{% 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 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 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.options }},
          {{ pin_option }}={ {{ 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 %}
