4

I'm having an issue with the order of layers in the second figure. In it, the lines and patterns for the plots are drawn on a lawyer below the gridlines. Whereas the first figure's line and markers are correctly drawn on top of the gridlines. I've tried several things with layer configuration to no avail.

How can I get the plotted lines and pattern to draw on top of the gridlines?

https://www.overleaf.com/read/yxjnwmtzcrnt#19a1b6

\documentclass{article}
\usepackage{
  pgfplots,
  pgfplotstable,
  tikz,
}
\pgfplotsset{compat=1.18}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\pgfdeclarepattern{%
    name=hatch,
    parameters={\hatchsize,\hatchangle,\hatchlinewidth},
    bottom left={\pgfpoint{-.1pt}{-.1pt}},
    top right={%
            \pgfpoint{\hatchsize+.1pt}{\hatchsize+.1pt}
        },
    tile size={\pgfpoint{\hatchsize}{\hatchsize}
        },
    tile transformation={\pgftransformrotate{\hatchangle}},
    code={%
            \pgfsetlinewidth{\hatchlinewidth}
            \pgfpathmoveto{\pgfpoint{-.1pt}{\hatchsize/2}}
            \pgfpathlineto{\pgfpoint{\hatchsize+.1pt}{\hatchsize/2}}
            \pgfusepath{stroke}
        }
}
\tikzset{%
    hatch size/.store in=\hatchsize,
    hatch angle/.store in=\hatchangle,
    hatch line width/.store in=\hatchlinewidth,
    hatch size=25pt,
    hatch angle=0pt,
    hatch line width=-.5pt,
}
\begin{document}
\begin{filecontents*}{data.csv}
year,green,closed,bronze,gold,hybrid
1969,11737,710250,70339,6530,11809
1970,13078,817657,80159,13628,23032
1971,12534,780263,69704,4407,8757
1972,14352,814319,71938,4542,9082
1973,13628,837502,73691,4588,9324
1974,14850,865463,75067,5029,9505
1975,16481,906618,77047,4759,9401
1976,19448,925463,81885,4401,9721
1977,22602,961125,86952,4388,10345
1978,24312,1000794,90563,4490,11344
1979,25681,1046620,90581,4990,13207
1980,27595,1152113,95524,5163,13620
1981,25065,1135597,101183,5537,14099
1982,24645,1173917,100325,5612,14324
1983,24922,1216495,103695,6633,15036
1984,26223,1261813,112007,7090,15432
1985,26656,1313175,114293,7067,15784
1986,28650,1385096,116738,7645,17863
1987,30125,1462042,125438,8951,18054
1988,32891,1534839,128130,9083,19145
1989,38454,1632297,132983,10535,19911
1990,41391,1778483,140850,11373,22804
1991,42114,1777618,145960,12408,24062
1992,46914,1843584,150952,13924,24140
1993,51239,1926236,156990,14666,25578
1994,57455,2028220,162357,15946,28067
1995,62271,2143439,168962,16984,34860
1996,65004,2279232,202580,19381,37256
1997,69900,2385818,209907,23849,38059
1998,74403,2496139,227351,27527,40265
1999,81146,2627203,226052,32391,42572
2000,95428,3101304,248775,37925,41987
2001,103423,3221112,247080,40489,43629
2002,135901,3744344,264539,46654,46390
2003,140518,4000663,285383,52644,52654
2004,152074,4294002,309541,64303,59622
2005,176812,4583922,328903,78965,64118
2006,201593,5006254,350860,93331,70149
2007,222899,5476505,367614,109160,89808
2008,247650,5755354,379275,142427,100713
2009,278657,6181729,409119,181195,119145
2010,313062,6539984,436305,227547,138222
2011,335004,7068030,471832,279987,167883
2012,352466,7149388,518901,365676,192191
2013,382015,7492238,560712,448193,213692
2014,397364,7696012,575212,556367,242386
2015,421189,7572689,587524,766114,274653
2016,465629,7549915,665761,855288,322584
2017,518958,7186068,640937,1041694,332885
2018,579439,6715921,724592,1264180,392316
2019,629749,6580960,738856,1547252,400788
2020,772651,6439999,800954,1808933,630368
2021,830155,5142005,831607,1956323,699052
2022,936272,4053378,818028,1926910,1213021
2023,852427,4199630,1424973,1550481,929902
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfplotsset{%
  every axis plot/.append style={%
      smooth
    },
  height=\textheight/3,
  major grid style={%
      line width=.2pt,
      draw=gray!90
    },
  minor grid style={%
      line width=.1pt,
      draw=gray!50
    },
  width=\textwidth,
  xlabel={Year},
  xmajorgrids=true,
  xminorgrids=true,
  xticklabel style={%
      /pgf/number format/set thousands separator={},
      anchor=north
    },
  ymajorgrids=true,
  yminorgrids=true,
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[htbp]
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        legend pos=south east,
        mark options={%
            fill opacity=0.0,
            scale=0.5
          },
        xlabel={Year},
        xmin=1970,
        xmax=2020,
        ymode=log,
        xtick={1970,1980,...,2020},
        minor x tick num={9},
      ]
      \addplot+[%
        color=red,
      ] table [%
          x=year,
          y=closed,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Closed}
      \addplot+[%
        color=brown,
      ] table [%
          x=year,
          y=bronze,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Bronze}
      \addplot+[%
        color=green,
      ] table [%
          x=year,
          y=green,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Green}
      \addplot+[%
        color=blue,
      ] table [%
          x=year,
          y=hybrid,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Hybrid}
      \addplot+[%
        color=orange,
      ] table [%
          x=year,
          y=gold,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Gold}
    \end{axis}
  \end{tikzpicture}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{figure}[htbp]
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        area style,
        every axis plot post/.append style={%
            hatch size=10pt,
            hatch line width=0.1pt,
            pattern=hatch,
          },
        legend image code/.code={%
            \draw[#1,
              pattern=#1,
              pattern color=.!80!black
            ] (0cm,-0.25cm) rectangle (1cm,0.25cm);},
        legend pos=north west,
        minor x tick num={4},
        stack plots=y,
        xmin=1969,
        xmax=2020,
        xtick={1970,1975,...,2020},
        ymode=log,
      ]
      \addplot+[%
        color=red,
        pattern color=red,
        hatch angle=30
      ] table [
          x=year,
          y=closed,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Closed}
      \addplot+[%
        color=brown,
        pattern color=brown,
        hatch angle=60
      ] table [
          x=year,
          y=bronze,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Bronze}
      \addplot+[%
        color=green,
        pattern color=green,
        hatch angle=90
      ] table [
          x=year,
          y=green,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Green}
      \addplot+[%
        color=blue,
        pattern color=blue,
        hatch angle=120
      ] table [
          x=year,
          y=hybrid,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Hybrid}
      \addplot+[%
        color=orange,
        pattern color=orange,
        hatch angle=150
      ] table [
          x=year,
          y=gold,
          col sep=comma
        ]{data.csv}\closedcycle;\addlegendentry{Gold}
    \end{axis}
  \end{tikzpicture}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

1 Answer 1

5

The key area style sets axis on top to true and then the grid lines are drawn on top of the plot. A solution is to place axis on top=false, after area style,.

An alternative for xmajorgrids=true,xminorgrids=true,ymajorgrids=true,yminorgrids=true, is the key grid=both.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .