使用我从这里,我试图制作一个高度y = 6
介于边界x = 2
和之间的小矩形x = 7
,并将其遮蔽。
知道为什么这不起作用吗?
\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle, axis y line=middle,
ymin=0, ymax=10, ytick={0,2,...,10}, ylabel=$f(x)$,
xmin=0, xmax=10, xtick={0,2,...,10}, xlabel=$x$,
domain=-pi:pi,samples=101, % added
]
\addplot[domain=2:7,blue,name path=A] {6}; % actual curve
\addplot[draw=none,name path=B] {0}; % “fictional” curve
\addplot[gray] fill between[of=A and B,soft clip={domain=2:7}]; %filling
\addplot+[
blue,very thick,dotted,
mark=none,
const plot,
empty line=jump,
]
coordinates {
(2,0)
(2,6)
(7,0)
(7,6)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
它不起作用,因为domain=-pi:pi
您在轴选项中设置的域用于名为的图B
。如果您为该图添加适当的域,它将按预期工作。
\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle, axis y line=middle,
ymin=0, ymax=10, ytick={0,2,...,10}, ylabel=$f(x)$,
xmin=0, xmax=10, xtick={0,2,...,10}, xlabel=$x$,
domain=-pi:pi,samples=101, % added
]
\addplot[domain=2:7,blue,name path=A] {6}; % actual curve
\addplot[draw=none,name path=B,domain=2:7] {0};
\addplot[gray] fill between[of=A and B,soft clip={domain=2:7}]; %filling
\addplot+[
blue,very thick,dotted,
mark=none,
const plot,
empty line=jump,
]
coordinates {
(2,0)
(2,6)
(7,0)
(7,6)
};
\end{axis}
\end{tikzpicture}
\end{document}
附录:只是为了向 Stefan Pinnow 表明他的提议肯定不是最简单的。此代码
\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-euclide}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle, axis y line=middle,
ymin=0, ymax=10, ytick={0,2,...,10}, ylabel=$f(x)$,
xmin=0, xmax=10, xtick={0,2,...,10}, xlabel=$x$,
domain=-pi:pi,samples=101, % added
]
\fill[gray] (2,0) rectangle (7,6);
\addplot+[
blue,very thick,dotted,
mark=none,
const plot,
empty line=jump,
]
coordinates {
(2,0)
(2,6)
(7,0)
(7,6)
};
\end{axis}
\end{tikzpicture}
\end{document}
输出结果相同。但这是不是讨论的重点。我认为,重点在于您的代码产生了意想不到的结果(或者在编辑之前被称为“时髦”的结果)。要对矩形进行着色,您甚至不需要 pgfplots,实际上甚至不需要 Ti钾Z。
答案2
正如 marmot 指出的那样他的回答造成混乱的原因是这条道路乙有错误的。获取所需内容的最简单方法是(仅)在选项中domain
设置,然后在其他所有位置将其删除。domain
axis
但要简单地绘制一个矩形,则不需要该fillbetween
库。有关详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{width=10cm,compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
ymin=0,
ymax=10,
ytick={0,2,...,10},
ylabel=$f(x)$,
xmin=0,
xmax=10,
xtick={0,2,...,10},
xlabel=$x$,
domain=2:7, % <-- adapted to the "right" bounds
samples=2, % <-- (for straight lines 2 is enough)
]
% alternative without using the `fillbetween' library
\addplot [fill=red,opacity=0.5] coordinates { (2,6) (7,6) }
\closedcycle
;
\addplot [blue,name path=A] {6};
\addplot [draw=none,name path=B] {0};
\addplot [gray] fill between [
of=A and B,
% soft clip={domain=2:7} % <-- not needed any more now
];
\addplot+ [
blue,very thick,dotted,
mark=none,
const plot,
empty line=jump,
] coordinates {
(2,0)
(2,6)
(7,0)
(7,6)
};
\end{axis}
\end{tikzpicture}
\end{document}