最近我发现了 Stack Exchange 社区提供的一个很好的例子,fillbetween
用于填充曲线和常数(见第一张图片)。我特别喜欢它,因为区域的颜色会根据曲线大于或小于常数而变化。这是通过findintersections
Jake 提供的功能实现的(解决方案链接)。不过,我认为如果它能填充两条曲线(见第二幅图)。我在下面提供了一个 MWE,它有两条曲线和一个常数。有人能提供一个解决方案,将两条曲线相互比较吗?然后根据哪条曲线大于另一条曲线,更改曲线之间的颜色?我认为这样做对于难以解释的图表非常有用,因为两条曲线之间的差异很小,而且重叠很多。
梅威瑟:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usetikzlibrary{calc}
\begin{filecontents}{data.dat}
0 0.2
1 0.217
2 0.255
3 0.288
6 0.58
7 0.91
8 1.02
10 1.05
12 0.92
13 0.78
15 0.56
17 1.1
\end{filecontents}
\begin{filecontents}{data2.dat}
0 1.2
1 0.8
2 0.6
3 0.20
6 0.4
7 0.5
8 1.5
10 1.5
12 0.5
13 0.9
15 0.4
17 1.1
\end{filecontents}
\pgfplotstableread{data.dat}\data
\pgfplotstableread{data2.dat}\datas
\newcommand\findintersections[2]{
\def\prevcell{#1}
\pgfplotstableforeachcolumnelement{1}\of#2\as\cell{%
\pgfmathparse{!or(
and(
\prevcell>#1,\cell>#1
),
and(
\prevcell<#1,\cell<#1
)
)}
\ifnum\pgfmathresult=1
\pgfplotstablegetelem{\pgfplotstablerow}{0}\of{\data} \let\xb=\pgfplotsretval
\pgfplotstablegetelem{\pgfplotstablerow}{1}\of{\data} \let\yb=\pgfplotsretval
\pgfmathtruncatemacro\previousrow{ifthenelse(\pgfplotstablerow>0,\pgfplotstablerow-1,0)}
\pgfplotstablegetelem{\previousrow}{0}\of{\data} \let\xa=\pgfplotsretval
\pgfplotstablegetelem{\previousrow}{1}\of{\data} \let\ya=\pgfplotsretval
\pgfmathsetmacro\newx{
\xa+(\ya-#1)/(ifthenelse(\yb==\ya,1,\ya-\yb) )*(\xb-\xa) }
\edef\test{\noexpand\pgfplotstableread[col sep=comma,row sep=crcr,header=has colnames]{
0,1\noexpand\\
\newx,#1\noexpand\\
}\noexpand\newrow}
\test
\pgfplotstablevertcat\interpolated{\newrow}
\fi
\let\prevcell=\cell
}
\pgfplotstablevertcat\interpolated{#2}
\pgfplotstablesort[sort cmp={float <}]\interpolated{\interpolated}
\pgfplotstableset{
create on use/above line/.style={
create col/expr={max(\thisrow{1},#1)}
},
create on use/below line/.style={
create col/expr={min(\thisrow{1},#1)}
},
}
}
\begin{document}
\pgfplotsset{compat=newest} % For nicer label placement
\findintersections{0.9}{\data}
\begin{tikzpicture}
\begin{axis}[
xlabel=Time of day,
ylabel=Volume,
ytick=\empty,
axis x line=bottom,
axis y line=left,
enlargelimits=true
]
\addplot[fill,gray!20!white,no markers,line width=2pt] table [y=above line] {\interpolated} |- (current plot begin);
\addplot[fill,yellow!20!white,no markers,line width=2pt] table [y=below line] {\interpolated} |- (current plot begin);
\addplot[orange,no markers,line width=2pt,domain=-1:20] {0.9};
\addplot[blue,line width=2pt,mark=*] table {\data};
\addplot[red,line width=2pt,mark=*] table {\datas};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
从pgfplots
1.10 版本开始您可以加载库fillbetween
:
\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}% current version is 1.15
\usepgfplotslibrary{fillbetween}
\usepackage{filecontents}
\begin{filecontents}{data.dat}
0 0.2
1 0.217
2 0.255
3 0.288
6 0.58
7 0.91
8 1.02
10 1.05
12 0.92
13 0.78
15 0.56
17 1.1
\end{filecontents}
\begin{filecontents}{data2.dat}
0 1.2
1 0.8
2 0.6
3 0.20
6 0.4
7 0.5
8 1.5
10 1.5
12 0.5
13 0.9
15 0.4
17 1.1
\end{filecontents}
\pgfplotstableread{data.dat}\data
\pgfplotstableread{data2.dat}\datas
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Time of day,
ylabel=Volume,
ytick=\empty,
axis x line=bottom,
axis y line=left,
enlargelimits=true
]
\addplot[name path=plot1,blue,line width=2pt,mark=*] table {\data};
\addplot[name path=plot2,red,line width=2pt,mark=*] table {\datas};
\addplot fill between[
of = plot1 and plot2,
split, % calculate segments
every even segment/.style = {yellow!20!white},
every odd segment/.style ={gray!20!white}
];
\end{axis}
\end{tikzpicture}
\end{document}
结果是
如果axis background
可以填充白色,而您只想将红色曲线高于蓝色曲线的区域涂成黄色,则可以使用
\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}% current version is 1.15
\usepgfplotslibrary{fillbetween}
\usepackage{filecontents}
\begin{filecontents}{data.dat}
0 0.2
1 0.217
2 0.255
3 0.288
6 0.58
7 0.91
8 1.02
10 1.05
12 0.92
13 0.78
15 0.56
17 1.1
\end{filecontents}
\begin{filecontents}{data2.dat}
0 1.2
1 0.8
2 0.6
3 0.288
4 1
6 0.4
7 0.5
8 1.5
10 1.5
12 0.5
13 0.9
15 0.4
17 1.1
\end{filecontents}
\pgfplotstableread{data.dat}\data
\pgfplotstableread{data2.dat}\datas
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Time of day,
ylabel=Volume,
ytick=\empty,
axis x line=bottom,
axis y line=left,
enlargelimits=true,
axis background/.style={fill=white}
]
\addplot[name path=plot1,blue,line width=2pt,mark=*] table {\data};
\addplot[name path=plot2,red,line width=2pt,mark=*] table {\datas};
\path[name path=xaxis](current axis.south west)--(current axis.south east);
\addplot[yellow!20!white] fill between[
of = plot1 and plot2,
split
];
\addplot[axis background] fill between[of = plot1 and xaxis];
\end{axis}
\end{tikzpicture}
\end{document}
结果是(我改变了数据文件中的某些值)
不幸的是,我没有找到一个解决方案,可以在同一张图片中将红色曲线较高的区域涂成黄色,将红色曲线较低的区域涂成灰色。
答案2
您可以使用every segment no <index>
此处的语法来指定每个填充段的格式。请参阅手册的第 4.5.10 节和第 5.7.2 - 5.7.4 节PgfPlots
。
另一个选项是为路径创建两个数据系列,定义两组数据之间差异的上限和下限。它们
可以用作定义填充区域的辅助程序,就像在 esdd 的答案中使用 x 轴作为辅助程序一样,但这里不需要 x 轴辅助程序。
这是 MWE:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest} % For nicer label
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xcolor}
\colorlet{higher}{yellow!30}
\colorlet{lower}{lightgray}
\begin{filecontents}{data.dat}
0 0.2
1 0.217
2 0.255
3 0.288
6 0.58
7 0.91
8 1.02
10 1.05
12 0.92
13 0.78
15 0.56
17 1.1
\end{filecontents}
\begin{filecontents}{data2.dat}
0 1.2
1 0.8
2 0.6
3 0.20
6 0.4
7 0.5
8 1.5
10 1.5
12 0.5
13 0.9
15 0.4
17 1.1
\end{filecontents}
\pgfplotstableread{data.dat}\data
\pgfplotstableread{data2.dat}\datas
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Time of day,
ylabel=Volume,
ytick=\empty,
axis x line=bottom,
axis y line=left,
enlargelimits=true
]
\addplot[name path=plot1,blue,line width=2pt,mark=*] table {\data};
\addplot[name path=plot2,red,line width=2pt,mark=*] table {\datas};
\addplot
fill between[of = plot1 and plot2,
split,
every segment no 0/.style={fill=higher},
every segment no 1/.style={fill=lower},
every segment no 2/.style={fill=higher},
every segment no 3/.style={fill=lower},
every segment no 4/.style={fill=higher},
every segment no 5/.style={fill=lower},
]
;
\end{axis}
\end{tikzpicture}
\end{document}