我使用来\fillbetween
填充下面显示的两条曲线之间。
它在负 x 轴上的曲线之间填充得非常奇怪。有人知道为什么吗?
这是我使用的代码
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfkeys{/pgf/number format/.cd,1000 sep={}}
\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo
\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\normalsize},
tick label style={font=\normalsize}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=middle,
enable tick line clipping=false,
axis line style=semithick,
width=9cm,
height=9cm,
x tick style={black,semithick},
x label style=
{at={(current axis.right of origin)},anchor=west},
xlabel={$x$},
xmin=-20,xmax=20,
xtick={-15,-10,...,15},
xtick pos=bottom,
y tick style={black,semithick},
y label style=
{at={(current axis.above origin)},anchor=south},
ylabel={$y$},
ymin=0,ymax=40,
ytick={-10,0,10,...,30},
]
\addplot [blue,very thick,name path=A] table{
x y
8 0
9.5 8
13.5 28
13.5 37
};
\addplot [blue,very thick,name path=B] table{
x y
10 0
10 8
14 28
14 37
};
\addplot [blue!20] fill between [of=A and B];
\addplot [blue,very thick,name path=A1] table{
x y
-10 0
-10 8
-14 28
-14 37
};
\addplot [blue,very thick,name path=B1] table{
x y
-8 0
-9.5 8
-13.5 28
-13.5 37
};
\addplot [blue!20] fill between [of=A1 and B1];
\draw [red,thick,dashed] (-20,28) -- (20,28);
\draw [red,thick,dashed] (-20,8) -- (20,8);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
“有人知道为什么吗?”这个问题的答案是“是的”。为了填充得不那么尴尬,你需要reverse=true
填充左边的区域。为什么?因为手册在第 439 页上说
如果 pgfplots 选择了错误的,则需要手动撤销。
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}%fitting functions
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{pgfplotstable}
\usepgfplotslibrary{fillbetween}
\pgfkeys{/pgf/number format/.cd,1000 sep={}}
\newcommand{\myfont}{\fontfamily{cmss}\selectfont}% used with mathpazo
\pgfplotsset{compat=1.15}
\pgfplotsset{label style={font=\normalsize},
tick label style={font=\normalsize}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=middle,
enable tick line clipping=false,
axis line style=semithick,
width=9cm,
height=9cm,
x tick style={black,semithick},
x label style=
{at={(current axis.right of origin)},anchor=west},
xlabel={$x$},
xmin=-20,xmax=20,
xtick={-15,-10,...,15},
xtick pos=bottom,
y tick style={black,semithick},
y label style=
{at={(current axis.above origin)},anchor=south},
ylabel={$y$},
ymin=0,ymax=40,
ytick={-10,0,10,...,30},
]
\addplot [blue,very thick,name path=A] table{
x y
8 0
9.5 8
13.5 28
13.5 37
};
\addplot [blue,very thick,name path=B] table{
x y
10 0
10 8
14 28
14 37
};
\addplot [blue!20] fill between [of=A and B];
\addplot [blue,very thick,name path=C] table{
x y
-10 0
-10 8
-14 28
-14 37
};
\addplot [blue,very thick,name path=D] table{
x y
-8 0
-9.5 8
-13.5 28
-13.5 37
};
\addplot [blue!20] fill between [of=D and C,reverse=true];
\draw [red,thick,dashed] (-20,28) -- (20,28);
\draw [red,thick,dashed] (-20,8) -- (20,8);
\end{axis}
\end{tikzpicture}
\end{document}