我正在对数刻度上绘制 y 轴,许多 y 值都小于一。
使用以下代码:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots} % drawing plots right here in this file!
\pgfplotsset{compat=1.9} % latest stable release
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{loglogaxis}
\addplot+[mark=none,domain=0.1:10] {x} ;
\addplot[domain=0.3:7,fill=black] {x} \closedcycle;
\end{loglogaxis}
\end{tikzpicture}
\end{document}
显然,循环从“log(y) = 0”开始,因此一些值填充在函数上方,一些值填充在函数下方。
我怎样才能让它在函数下方绘制所有 y 值,并让填充达到 y 轴上的最小值?
答案1
使用log origin=infty
键loglogaxis
。这会将轴上的“零”设置为负无穷大。为了完整起见,我应该指出,您的描述和您提供的 MWE 不一致(半对数与对数对数、图等)。我根据 MWE 回答,但它应该可以适应您的实际用例。
代码
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots} % drawing plots right here in this file!
\pgfplotsset{compat=1.9} % latest stable release
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{loglogaxis}[log origin=infty] % <<< added key here
\addplot+[mark=none,domain=0.1:10] {x} ;
\addplot[domain=0.3:7,fill=black] {x} \closedcycle;
\end{loglogaxis}
\end{tikzpicture}
\end{document}
输出
替代解决方案使用fillbetween
:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots} % drawing plots right here in this file!
\pgfplotsset{compat=1.9} % latest stable release
\usepgfplotslibrary{fillbetween}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{loglogaxis}
\addplot+[mark=none,domain=0.1:10] {x} ;
\addplot[domain=0.3:7,name path=A] {x};
\addplot[domain=0.3:7,name path=B,update limits=false] {0.05}; % this must be below the axis limit
\addplot[black] fill between[of=A and B]; % fill between paths A and B
\end{loglogaxis}
\end{tikzpicture}
\end{document}
输出
(与上文相同,但在填充顶部有刻度标记。)
答案2
技巧的使用clip
。代码和图像似乎属于不同的坐标系——loglog
和semilog
。因此,此解决方案根据坐标系提供两个结果。关键概念是设置一个剪辑矩形,然后设置带有的闭合曲线x axis
。这两个命令形成的这两个区域的交点将是所需的结果。
代码
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots} % drawing plots right here in this file!
\pgfplotsset{compat=1.8} % latest stable release
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{loglogaxis}
\begin{scope}
\clip (axis cs: 0.1,0.1) rectangle (axis cs:10,10);
\addplot+[mark=none,domain=0.1:10,fill=blue!20!white] {x} -- (axis cs: 0.1,0.1) --(axis cs: 10,0.1) --(axis cs: 10,10);
% \addplot[domain=0.3:7,fill=black] {x} \closedcycle;
\end{scope}
\end{loglogaxis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{semilogyaxis}
\begin{scope}
\clip[] (axis cs: 0,0.1) rectangle (axis cs:10,10);
\addplot+[mark=none,domain=0.1:10,fill=blue!20!white] {x} -- (axis cs: 10,0.1) --(axis cs: 0.1,0.1);
%% \addplot[domain=0.3:7,fill=black] {x} \closedcycle;
\end{scope}
\end{semilogyaxis}
\end{tikzpicture}
\end{document}
更新:如果希望一直到 x 轴的区域都一致,那么方法如下。关键在于正确选择坐标。
代码
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots} % drawing plots right here in this file!
\pgfplotsset{compat=1.8} % latest stable release
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}
\begin{loglogaxis}
\begin{scope}
\clip (axis cs: 0.1,0.01) rectangle (axis cs:10,10);
\addplot+[mark=none,domain=0.1:10,fill=blue!20!white] {x} --(axis cs: 10,0.05) -- (axis cs: 0.1,0.05);
% \addplot[domain=0.3:7,fill=black] {x} \closedcycle;
\end{scope}
\draw[white](axis cs: 10,10)--(axis cs:10,0.05);
\end{loglogaxis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{semilogyaxis}
\begin{scope}
\clip (axis cs: 0,0.065) rectangle (axis cs:10,10);
\addplot+[mark=none,domain=0.1:10,fill=blue!20!white,smooth,draw] {x} -- (axis cs: 10,0.06) -- (axis cs: 0.1,0.06);
%% \addplot[domain=0.3:7,fill=black] {x} \closedcycle;
\end{scope}
\draw[white](axis cs: 10,10)--(axis cs:10,0.065);
\end{semilogyaxis}
\end{tikzpicture}
\end{document}