我使用ticklabel style={fill=white}
来pgfplots
确保刻度标签在网格线中仍然清晰可读。但是,当我使用 时,似乎fillbetween
sticklabel
会在填充之后绘制,因此会显示在图表顶部。如果我使用命令\draw
绘制矩形,则矩形最终会显示在 的顶部ticklabel
,这正是我想要的。
下面是我正在使用的代码和图
\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}
答案1
这是因为默认情况下fill between
,内容绘制在图层上pre main
,其余内容也绘制在图层上main
。要获得所需的结果,只需使用预定义图层即可standard
。
(请注意,我还允许自己“优化”\addplot
一下您的命令。有关更多详细信息,请查看代码中的注释。)
我还包括了仅填充“封闭”区域的额外要求,如该答案下方的评论中所写。
\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}