2

I am using ticklabel style={fill=white} with pgfplots to ensure my tick labels are readable despite my grid-lines. However, it seems that when I use fillbetween the ticklabels get drawn after the fill and therefore show up on top of my diagram. If I use a \draw command to draw a rectangle, the rectangle ends up on top of the ticklabel, which is what I want.

Below is the code I am using and a figure

\begin{tikzpicture}
    \begin{axis}[
        xmin=-2,xmax=2,
        ymin=-1,ymax=2,
        x=1cm,y=1cm,
        axis lines=middle,
        grid=both,
        minor tick num=0,
        enlargelimits={abs=0.5},
        ticklabel style={font=\tiny,fill=white},
    ]

        \addplot[name path=f,no marks,mypink,thick,domain=-2:2, samples=25, draw=none] ({x},{x*x});
        \addplot[no marks,thick,domain=-2:2, samples=25,smooth] ({x},{x*x});
        \addplot[name path=g, no marks,thick,domain=-5:5, samples=2] ({x},{1.5});
        \addplot [thick, color=blue, fill=blue, fill opacity=0.5] fill between [of=f and g, split];

        \draw[fill=blue,opacity=0.5] (-1.5,-.5) rectangle(-.5,.5);

    \end{axis}
\end{tikzpicture}

enter image description here

| improve this question | | | | |
4

This is because the fill between stuff is drawn on the layer pre main by default and all the rest on the layer main. To get your desired result just use the predefined layers standard.

(Please note that I also allowed myself to "optimize" your \addplot commands a bit. For more details have a look at the comments in the code.)

I also included the additional requirement of just filling the "closed" area as written in the comments below this answer.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{compat=1.11}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            set layers=standard,    % <-- added
            xmin=-2,xmax=2,
            ymin=-1,ymax=2,
            x=1cm,y=1cm,
            axis lines=middle,
            grid=both,
            minor tick num=0,
            enlargelimits={abs=0.5},
            ticklabel style={font=\tiny,fill=white},
            no markers,             % <-- moved common `\addplot' options here
            smooth,                 % <-- added, because it looks a bit "smoother"
        ]

            % combined first two `\addplot's to this one
            % and you don't need a parametric plot here
            \addplot [name path=f,draw=black,thick,domain=-2:2, samples=25] {x^2};
            \addplot [name path=g,thick,domain=-5:5, samples=2] {1.5};

            \addplot [fill=none] fill between [
                    of=f and g,
                    split,
                    % draw only selected ones:
                    % every segment no 0/.style: invisible
                    every segment no 1/.style={
                        fill=blue,opacity=0.5
                    },
                    % every segment no 2/.style: invisible
                ];

            \draw[fill=blue,opacity=0.5] (-1.5,-.5) rectangle(-.5,.5);

        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

| improve this answer | | | | |
  • Exactly what I needed! In my original, I was only shading the bounded region which is why I had two different plots (one had a restricted domain). Is there a way to shade only the bounded region but still have only one curve for each? – Jason Siefken Dec 12 '16 at 19:29
  • Do you mean with "bounded area" only the "closed curve" between the x values -1.22 and 1.22? But then I don't know what you mean with "only one curve for each". – Stefan Pinnow Dec 12 '16 at 19:34
  • I wanted to shade the region whose x coords were between -1.22 and 1.22. I still wanted to draw the whole curve, so I used an invisible plot with domain -1.22 to 1.22 for the fillbetween and a visible plot with full domain to draw the curve. – Jason Siefken Dec 12 '16 at 19:37
  • Of course you can do that. Have a look at my edited answer. – Stefan Pinnow Dec 12 '16 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.