我尝试使用 pgfplotslibrary fillbetween 填充特定域中 pgfplot 图中两个图之间的区域,但无法获得结果。
也许你们中有人会说我的代码中到底做错了什么?
梅威瑟:
\documentclass[xcolor={svgnames}]{beamer}
\usepackage[german]{babel}
\usepackage[svgnames]{xcolor}
\usepackage[eulergreek]{sansmath}
\usepackage{calc}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{%
compat=newest,tick label style={font=\sffamily},
xticklabel={\pgfmathprintnumber[assume math mode=true]{\tick}},
yticklabel={\pgfmathprintnumber[assume math mode=true]{\tick}},
}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{backgound,main,foregound}
\usetikzlibrary{%
backgrounds,
arrows,
spy,
backgrounds,
external,
plotmarks
}
\begin{filecontents*}{minmax1.csv}
x1 ; x2 ; x3 ; x4 ; x5 ; x6 ; x7 ; x8 ; x9 ; x10
4,5 ; 0,33 ; 0,36 ; 0,33 ; 0,36 ; 0,36 ; 0,4 ; 1 ; 0,34 ; 0,35
4,8 ; 0,34 ; 0,38 ; 0,34 ; 0,375 ; 0,36 ; 0,4 ; 1 ; 0,34 ; 0,37
5 ; 0,35 ; 0,385 ; 0,35 ; 0,385 ; 0,36 ; 0,4 ; 0,5 ; 0,34 ; 0,38
5,2 ; 0,37 ; 0,4 ; 0,37 ; 0,4 ; 0,36 ; 0,4 ; 0 ; 0,34 ; 0,39
5,5 ; 0,375 ; 0,41 ; 0,375 ; 0,41 ; 0,36 ; 0,4 ; 0 ; 0,34 ; 0,41
\end{filecontents*}
\begin{document}
\begin{frame}{}
\begin{overprint}
\begin{tikzpicture}
\begin{axis}[
cycle list={%
{DarkGreen,solid, thick},
{DarkBlue,solid, thick},
{gray!50,solid, thin},
{red,dashed,thin},
{red,dashed,thin},
{red,dashed,thin},
},
font={\sffamily},
every axis label/.append style={font=\sffamily\footnotesize},
width=\textwidth,%Breite des Plots
axis line style=thin,%Achsenstärke
axis line style={gray!30},
axis y line = center,
axis x line = center,
tick label style={font={\sansmath\sffamily\fontsize{4}{4}\selectfont}},
x label style={at={(axis description cs:1.0,-0.03)},anchor=north, font={\tiny}},
y label style={at={(axis description cs:-0.08,.5)},rotate=90,anchor=south, font={\tiny}},
every y tick/.style={gray!30},
ytick={0.3,0.31,...,0.5},
extra y ticks = {0.31,0,32,...,0.5},
extra y tick labels= \empty,
every x tick/.style={gray!30},
xtick={0,0.1,...,5.5},
extra x ticks = {0,0.1,...,15},
extra x tick labels= \empty,
no markers,%keine Markierungen auf den Plots
every axis plot/.append style={thin},%Plotstärke
every axis/.append style={font=\sffamily},%Plotstärke
axis on top=false,%Achsen hinter dem Plot
font={\sffamily},
]
%%Plots
\addplot table[col sep=semicolon,/pgf/number format/read comma as period, x index = {0}, y index = {2}]{minmax1.csv};
\addplot table[name path=F, col sep=semicolon,/pgf/number format/read comma as period, x index = {0}, y index = {3}]{minmax1.csv};
\addplot table[col sep=semicolon,/pgf/number format/read comma as period, x index = {0}, y index = {9}]{minmax1.csv};
\addplot table[name path=G, col sep=semicolon,/pgf/number format/read comma as period, x index = {0}, y index = {5}]{minmax1.csv};
\addplot table[col sep=semicolon,/pgf/number format/read comma as period, x index = {0}, y index = {6}]{minmax1.csv};
%This is what does not work:
%\addplot[fill, color=brown!50]fill between[of=F and G, soft clip={domain=4.9:5.1}];
%This is for the blue lines in the background
\scoped[on background layer]\draw[color=blue, dotted] (axis cs:5.0,0.33) -- (axis cs:5.0,0.41);
\scoped[on background layer]\draw[color=blue!50, dotted] (axis cs:4.8,0.33) -- (axis cs:4.8,0.41);
\scoped[on background layer]\draw[color=blue!50, dotted] (axis cs:5.2,0.33) -- (axis cs:5.2,0.41);
\end{axis}
\end{tikzpicture}
\end{overprint}
\end{frame}
\end{document}
这正是我真正想要实现的目标:
我还想用居中的文本标记一个区域,并用箭头指向该区域的边缘。有人能告诉我怎么做吗?
答案1
(我在运行您的示例时遇到了一些麻烦。我不得不将数据文件中的逗号改为句点,并删除多余的刻度(这本来就是毫无意义的)以避免出现维度过大的错误。因此代码做了一些改变。)
你的错误可能是你添加了而不是name path
作为一个选项。结果,路径实际上没有命名,并且不起作用。你需要table
\addplot
fill between
\addplot [name path=F] table[x index=...]
并不是
\addplot table[name path=F,x index=...]
至于你想要的箭头,你可以做类似的事情
\draw [Stealth-Stealth] (4.8,0.335) --node[fill=white,align=center,font=\tiny]{Something\\like this} (5.2,0.335);
就在之前\end{axis}
。
箭头Stealth
提示来自较新的arrows.meta
库。您加载的旧arrows
库被视为已弃用。我没有axis cs
在坐标中使用,是因为带有compat=1.11
或较新的axis cs
是坐标的默认设置axis
,因此您无需明确指定它。
\documentclass[xcolor={svgnames}]{beamer}
\usepackage[german]{babel}
\usepackage[eulergreek]{sansmath}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{%
compat=1.15, %better to use explicit number
tick label style={font=\sffamily},
xticklabel={\pgfmathprintnumber[assume math mode=true]{\tick}},
yticklabel={\pgfmathprintnumber[assume math mode=true]{\tick}},
}
\usetikzlibrary{%
arrows.meta, %arrows is deprecated
% spy,
backgrounds,
external,
% plotmarks
}
\usepackage{filecontents}
\begin{filecontents*}{minmax1.csv}
x1 ; x2 ; x3 ; x4 ; x5 ; x6 ; x7 ; x8 ; x9 ; x10
4.5 ; 0.33 ; 0.36 ; 0.33 ; 0.36 ; 0.36 ; 0.4 ; 1 ; 0.34 ; 0.35
4.8 ; 0.34 ; 0.38 ; 0.34 ; 0.375 ; 0.36 ; 0.4 ; 1 ; 0.34 ; 0.37
5 ; 0.35 ; 0.385 ; 0.35 ; 0.385 ; 0.36 ; 0.4 ; 0.5 ; 0.34 ; 0.38
5.2 ; 0.37 ; 0.4 ; 0.37 ; 0.4 ; 0.36 ; 0.4 ; 0 ; 0.34 ; 0.39
5.5 ; 0.375 ; 0.41 ; 0.375 ; 0.41 ; 0.36 ; 0.4 ; 0 ; 0.34 ; 0.41
\end{filecontents*}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
cycle list={%
{DarkGreen,solid, thick},
{DarkBlue,solid, thick},
{gray!50,solid, thin},
{red,dashed,thin},
{red,dashed,thin},
{red,dashed,thin},
},
font={\sffamily},
every axis label/.append style={font=\sffamily\footnotesize},
width=\textwidth,%Breite des Plots
axis line style=thin,%Achsenstärke
axis line style={gray!30},
axis y line = center,
axis x line = center,
tick label style={font={\sansmath\sffamily\fontsize{4}{4}\selectfont}},
x label style={at={(axis description cs:1.0,-0.03)},anchor=north, font={\tiny}},
y label style={at={(axis description cs:-0.08,.5)},rotate=90,anchor=south, font={\tiny}},
every y tick/.style={gray!30},
ytick={0.3,0.31,...,0.5},
xtick={0,0.1,...,5.5},
no markers,%keine Markierungen auf den Plots
every axis plot/.append style={thin},%Plotstärke
every axis/.append style={font=\sffamily},%Plotstärke
axis on top=false,%Achsen hinter dem Plot
font={\sffamily},
]
%%Plots
\addplot table[col sep=semicolon, x index = {0}, y index = {2}]{minmax1.csv};
\addplot[name path=F] table[col sep=semicolon, x index = {0}, y index = {3}]{minmax1.csv};
\addplot table[col sep=semicolon, x index = {0}, y index = {9}]{minmax1.csv};
\addplot[name path=G] table[col sep=semicolon, x index = {0}, y index = {5}]{minmax1.csv};
\addplot table[col sep=semicolon, x index = {0}, y index = {6}]{minmax1.csv};
\addplot [color=brown!50] fill between[of=F and G,soft clip={domain=4.9:5.1}];
%This is for the blue lines in the background
\begin{scope}%[on background layer] %didn't work, don't know why
\draw[color=blue, dotted] (axis cs:5.0,0.33) -- (axis cs:5.0,0.41);
\draw[color=blue!50, dotted] (axis cs:4.8,0.33) -- (axis cs:4.8,0.41);
\draw[color=blue!50, dotted] (axis cs:5.2,0.33) -- (axis cs:5.2,0.41);
\end{scope}
\draw [Stealth-Stealth] (4.8,0.335) --node[fill=white,align=center,font=\tiny]{Something\\like this} (5.2,0.335);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}