我想制作一种面积图,即两个数据集之间的差异,具有对数 Y 轴刻度。可以使用“堆叠图”选项吗?在手册中提供的示例中,默认基准级别当然是 0。首先,对数模式不太好,其次,我实际上会将其中一个图设为基线。有什么想法吗?
\documentclass[article]{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=1e-5, ymax=1e-3,
% ymode=log,
stack plots=y,
area style,
enlarge x limits=false]
\addplot coordinates
{(1,3.121e-5) (2,2.882e-5) (3,1.469e-4) (4,3.910e-5) (5,3.910e-5)
(6,1.220e-4)}
\closedcycle;
\addplot coordinates
{(1,3.121e-5) (2,2.882e-5) (3,1.469e-4) (4,3.910e-5) (5,3.910e-5)
(6,1.220e-4)}
\closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
答案1
新答案
我的第一个回答太快了,即使用堆叠图时 PGFPlots 中没有错误,所以这里是我修改后的答案:
关键是要知道/记住,当使用堆叠图时,给定的命令是在反转顺序。因此,重要的是要说明\addplot fill between
前\addplot
路径命名的命令。
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
pgfplots.fillbetween,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=1e-5,
ymax=1e-3,
ymode=log,
stack plots=y,
enlarge x limits=false,
no markers,
]
% because when "stacked" plots is used the commands are evaluated in
% reversed order, we need to place the `fill between' command *before*
% the corresponding named pathes!
\addplot [fill=green!20] fill between [of=A and B];
\addplot+ [very thick,name path=A] coordinates {
(1,3.121e-5) (2,2.882e-5) (3,1.469e-4)
(4,3.910e-5) (5,3.910e-5) (6,1.220e-4)
};
\addplot+ [very thick,name path=B] coordinates {
(1,3.121e-5) (2,2.882e-5) (3,1.469e-4)
(4,3.910e-5) (5,3.910e-5) (6,1.220e-4)
};
\end{axis}
\end{tikzpicture}
\end{document}
旧答案
正如 Torbjørn T. 在在问题下方评论(通常)fillbetween
图书馆将是解决你的请求的选择,但目前似乎有一个漏洞在与堆叠图一起使用时,该库中存在错误。所以我们必须找到另一种方法来解决您的问题。
我的代码注释中也解释了为什么您使用的方法\closedcycle
不起作用。
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=1e-5,
ymax=1e-3,
ymode=log,
stack plots=y,
area style,
enlarge x limits=false,
% so the axis lines are on the top of the "cycle" lines
axis on top,
]
\addplot coordinates {
(1,3.121e-5) (2,2.882e-5) (3,1.469e-4)
(4,3.910e-5) (5,3.910e-5) (6,1.220e-4)
}
% `\closedcycle' doesn't work here, because it would it cycles to
% zero, which is *above* the lines here. So we have to create this
% path by hand.
% `current plot begin' refers to the first coordinate and because
% we are in a logarithmic axis `/pgfplots/ymin' yields the (natural)
% logarithmic value. (This is around -11 in this case and thus would
% again be above the plot.) So we have to delogarithmize it.
|- (current plot begin |- {axis cs:0,e^(\pgfkeysvalueof{/pgfplots/ymin})})
;
\addplot coordinates {
(1,3.121e-5) (2,2.882e-5) (3,1.469e-4)
(4,3.910e-5) (5,3.910e-5) (6,1.220e-4)
}
|- (current plot begin |- {axis cs:0,e^(\pgfkeysvalueof{/pgfplots/ymin})})
;
\end{axis}
\end{tikzpicture}
\end{document}