我有一个时间序列图。现在我想使用矩形突出显示某些区域。由于矩形应该覆盖整个图并且我使用了放大限制,因此我尝试使用rel axis
命令(例如)来解决这个问题({axis cs:86.27,0}|-{rel axis cs:0,0})
。
\documentclass[border=5pt,tikz]{standalone}
\usepackage{siunitx}
\DeclareSIUnit\wn{\cm\tothe{-1}}
\usepackage{
pgfplots,
filecontents
}
\pgfplotsset{
compat=newest,
every tick label/.append style={font=\small},
every axis label/.append style={font=\large},
every axis/.append style={line width=0.5pt},
every major tick/.append style={
line width=0.5pt,
major tick length=3pt,
% tick align=outside,
black
},
every minor tick/.append style={
line width=0.5pt,
minor tick length=1.5pt,
black
},
every linear axis/.append style={/pgf/number format/1000 sep={\,}},
plot/.style={
scale only axis=true,
scale=\sc,
},
}
\usetikzlibrary{calc}
\begin{filecontents}[overwrite]{peak3.txt}
0 1620.68954 0.79213 1622.76557 0.12062
5 1619.7955 0.34975 1619.92276 0.43016
10 1618.04759 0.31042 1619.55825 0.287
15 1617.88205 0.09656 1619.28215 0.61612
20 1616.68597 0.34419 1619.18661 0.15289
25 1616.12155 0.09647 1618.62932 0.22816
30 1616.0003 0.06372 1617.68941 0.33931
35 1615.57737 0.12619 1615.77222 0.56178
40 1615.5724 0.23914 1616.20548 0.46981
45 1615.66172 0.18898 1615.57348 0.2827
50 1615.32729 0.06836 1615.67729 0.44124
55 1615.46503 0.10357 1615.125 0.1919
60 1615.52368 0.19132 1614.3899 0.35759
65 1615.49754 0.1619 1614.90156 0.44356
70 1615.5754 0.23436 1614.92835 0.2785
75 1615.43131 0.15515 1614.83321 0.28528
80 1615.48456 0.22911 1614.93878 0.2555
85 1615.61115 0.138 1615.31617 0.29742
90 1615.45136 0.36388 1615.12409 0.25968
95 1615.16383 0.30691 1615.19095 0.19081
\end{filecontents}
\begin{document}
\def\height{16cm}
\def\widtha{9cm}
\def\sc{0.8}
\def\enlarge{0.05}
\begin{tikzpicture}
\begin{axis}[
name=shift-peak3,
plot,
width=\widtha,
height={\height/2-\heighta/2},
enlargelimits={\enlarge},
axis x line*=top,
xtick=\empty,
xmin=0,
xmax=100,
ymin=1614,
ymax=1624,
ytick pos=left,
ytick distance=2
]
\addplot[
color=black,
mark=*,
mark options={scale=\sc,fill=black},
restrict x to domain=0:100,
restrict y to domain=1614:1624,
]
table[
x index=0,
y index=1,
]{peak3.txt};
\node[font=\large,inner sep=0pt,anchor=north east] at (rel axis cs:0.975,0.975) {B};
\draw[color=green,opacity=0,fill=green,fill opacity=0.5] ({axis cs:10.37,0}|-{rel axis cs:0,0}) rectangle ({axis cs:16.17,0}|-{rel axis cs:0,1});
\draw[color=red,opacity=0,fill=red,fill opacity=0.5] ({axis cs:24.56,0}|-{rel axis cs:0,0}) rectangle ({axis cs:36.36,0}|-{rel axis cs:0,1});
\draw[color=brown,opacity=0,fill=brown,fill opacity=0.5] ({axis cs:84.5,0}|-{rel axis cs:0,0}) rectangle ({axis cs:85.91,0}|-{rel axis cs:0,1});
\draw[color=orange,opacity=0,fill=orange,fill opacity=0.5] ({axis cs:86.27,0}|-{rel axis cs:0,0}) rectangle ({axis cs:94.71,0}|-{rel axis cs:0,1});
\end{axis}
\end{tikzpicture}
\end{document}
执行此操作时,我收到错误dimensions too large
。我已经尝试限制 x 和 y 域,但这并不能解决问题。有人知道该怎么做吗?
编辑: 我改变了矩形的点,还添加了 x 标签等。
\documentclass[border=5pt,tikz]{standalone}
\usepackage{siunitx}
\DeclareSIUnit\wn{\cm\tothe{-1}}
\usepackage{pgfplots}
\pgfplotsset{
compat=newest,
every tick label/.append style={font=\small},
every axis label/.append style={font=\large},
every axis/.append style={line width=0.5pt},
every major tick/.append style={
line width=0.5pt,
major tick length=3pt,
black
},
every minor tick/.append style={
line width=0.5pt,
minor tick length=1.5pt,
black
},
every linear axis/.append style={/pgf/number format/1000 sep={\,}},
plot/.style={
scale only axis=true,
scale=\sc,
},
}
\usetikzlibrary{calc}
\begin{filecontents}[overwrite]{peak3.txt}
0 1620.68954 0.79213 1622.76557 0.12062
5 1619.7955 0.34975 1619.92276 0.43016
10 1618.04759 0.31042 1619.55825 0.287
15 1617.88205 0.09656 1619.28215 0.61612
20 1616.68597 0.34419 1619.18661 0.15289
25 1616.12155 0.09647 1618.62932 0.22816
30 1616.0003 0.06372 1617.68941 0.33931
35 1615.57737 0.12619 1615.77222 0.56178
40 1615.5724 0.23914 1616.20548 0.46981
45 1615.66172 0.18898 1615.57348 0.2827
50 1615.32729 0.06836 1615.67729 0.44124
55 1615.46503 0.10357 1615.125 0.1919
60 1615.52368 0.19132 1614.3899 0.35759
65 1615.49754 0.1619 1614.90156 0.44356
70 1615.5754 0.23436 1614.92835 0.2785
75 1615.43131 0.15515 1614.83321 0.28528
80 1615.48456 0.22911 1614.93878 0.2555
85 1615.61115 0.138 1615.31617 0.29742
90 1615.45136 0.36388 1615.12409 0.25968
95 1615.16383 0.30691 1615.19095 0.19081
\end{filecontents}
\begin{document}
\def\height{16cm}
\def\heighta{2cm}
\def\widtha{9cm}
\def\sc{0.8}
\def\enlarge{0.05}
\begin{tikzpicture}
\begin{axis}[
name=shift-peak3,
plot,
width=\widtha,
height={\height/2-\heighta/2},
enlargelimits={\enlarge},
xtick pos=left,
xmin=0,
xmax=100,
xtick distance=10,
xlabel={1,4 Dioxane\,/\,w\%},
ymin=1614,
ymax=1624,
ytick pos=left,
ytick distance=2,
ylabel={Peakposition\,/\,\si{\wn}},
]
\addplot[
color=black,
mark=*,
mark options={scale=\sc,fill=black},
]
table[
x index=0,
y index=1,
]{peak3.txt};
\node[font=\large,inner sep=0pt,anchor=north east] at (rel axis cs:0.975,0.975) {B};
\draw[color=green,opacity=1,fill=green,fill opacity=0.5] ({axis cs:10.37,0}|-{rel axis cs:0,0}) rectangle ({axis cs:16.17,0}|-{rel axis cs:0,1});
\draw[color=red,opacity=1,fill=red,fill opacity=0.5] ({rel axis cs:0.1037,0}) rectangle ({rel axis cs:0.1617,1});
% \draw[color=red,opacity=0,fill=red,fill opacity=0.5] ({axis cs:24.56,0}|-{rel axis cs:0,0}) rectangle ({axis cs:36.36,0}|-{rel axis cs:0,1});
% \draw[color=brown,opacity=0,fill=brown,fill opacity=0.5] ({axis cs:84.5,0}|-{rel axis cs:0,0}) rectangle ({axis cs:85.91,0}|-{rel axis cs:0,1});
% \draw[color=orange,opacity=0,fill=orange,fill opacity=0.5] ({axis cs:86.27,0}|-{rel axis cs:0,0}) rectangle ({axis cs:94.71,0}|-{rel axis cs:0,1});
\end{axis}
\end{tikzpicture}
\end{document}
正如您在图片中看到的,新的矩形(红色)现在不在之前的正确位置(绿色)。
答案1
我不确定你会用\def\height{16cm}
这条线做什么height={\height/2-\heighta/2},
,因为\heighta
没有定义(编辑:OP 提供的新版本代码使一切都变得清晰)。
我也对此行进行了评论restrict x to domain=...
,并且同样认为restrict y to domain=...
它无用(与问题无关)。
主要担心的是你奇怪的混合语法:
({axis cs:10.37,0}|-{rel axis cs:0,0}) rectangle ({axis cs:16.17,0}|-{rel axis cs:0,1});
用于显示彩色矩形。
使用语法:
(rel axis cs:0.1037,0) rectangle (rel axis cs:0.1617,1);
错误dimension too large
消失了(x 轴从 0 到 1 而不是从 0 到 100,因此 x=10.37 的值变成 x=0.1037,y 轴在坐标系中从 0 到 1 rel axis cs
)。
编辑:enlargelimits
这是不正确的,因为代码中有一个,所以rel axis cs
坐标系中的 x=0 与图中的 x=0 不一致。所以我用坐标系代替它axis cs
,以前的代码变成了(我选择的确定矩形的对立点的 y 值分别比代码中使用的和(axis cs:10.37,1612) rectangle (axis cs:16.17,1626);
的值小一点和大一点)。ymin
ymax
此外,该软件包filecontents
在最新的 LaTeX 内核中已经过时,因此,如果您的 LaTeX 发行版是最新的,您可以取消其加载,这样就可以避免出现有关它的警告信息。
我还选择在实验点之前绘制矩形,以便实验点不会被半透明矩形着色。
(更新后的)代码:
\documentclass[border=5pt,tikz]{standalone}
\usepackage{siunitx}
\DeclareSIUnit\wn{\cm\tothe{-1}}
\usepackage{pgfplots}
\pgfplotsset{
compat=newest,
every tick label/.append style={font=\small},
every axis label/.append style={font=\large},
every axis/.append style={line width=0.5pt},
every major tick/.append style={
line width=0.5pt,
major tick length=3pt,
black
},
every minor tick/.append style={
line width=0.5pt,
minor tick length=1.5pt,
black
},
every linear axis/.append style={/pgf/number format/1000 sep={\,}},
plot/.style={
scale only axis=true,
scale=\sc,
},
}
\usetikzlibrary{calc}
\begin{filecontents}[overwrite]{peak3.txt}
0 1620.68954 0.79213 1622.76557 0.12062
5 1619.7955 0.34975 1619.92276 0.43016
10 1618.04759 0.31042 1619.55825 0.287
15 1617.88205 0.09656 1619.28215 0.61612
20 1616.68597 0.34419 1619.18661 0.15289
25 1616.12155 0.09647 1618.62932 0.22816
30 1616.0003 0.06372 1617.68941 0.33931
35 1615.57737 0.12619 1615.77222 0.56178
40 1615.5724 0.23914 1616.20548 0.46981
45 1615.66172 0.18898 1615.57348 0.2827
50 1615.32729 0.06836 1615.67729 0.44124
55 1615.46503 0.10357 1615.125 0.1919
60 1615.52368 0.19132 1614.3899 0.35759
65 1615.49754 0.1619 1614.90156 0.44356
70 1615.5754 0.23436 1614.92835 0.2785
75 1615.43131 0.15515 1614.83321 0.28528
80 1615.48456 0.22911 1614.93878 0.2555
85 1615.61115 0.138 1615.31617 0.29742
90 1615.45136 0.36388 1615.12409 0.25968
95 1615.16383 0.30691 1615.19095 0.19081
\end{filecontents}
\begin{document}
\def\height{16cm}
\def\heighta{2cm}
\def\widtha{9cm}
\def\sc{0.8}
\def\enlarge{0.05}
\begin{tikzpicture}
\begin{axis}[
name=shift-peak3,
plot,
width=\widtha,
height={\height/2-\heighta/2},
enlargelimits={\enlarge},
xtick pos=left,
xmin=0,
xmax=100,
xtick distance=10,
xlabel={1,4 Dioxane\,/\,w\%},
ymin=1614,
ymax=1624,
ytick pos=left,
ytick distance=2,
ylabel={Peakposition\,/\,\si{\wn}},
]
\draw[color=green,opacity=0,fill=green,fill opacity=0.5] (axis cs:10.37,1612) rectangle (axis cs:16.17,1626);
\draw[color=red,opacity=0,fill=red,fill opacity=0.5] (axis cs:24.56,1612) rectangle (axis cs:36.36,1626);
\draw[color=brown,opacity=0,fill=brown,fill opacity=0.5] (axis cs:84.5,1612) rectangle (axis cs:85.91,1626);
\draw[color=orange,opacity=0,fill=orange,fill opacity=0.5] (axis cs:86.27,1612) rectangle (axis cs:94.71,1626);
\addplot[
color=black,
mark=*,
mark options={scale=\sc,fill=black},
]
table[
x index=0,
y index=1,
]{peak3.txt};
\node[font=\large,inner sep=0pt,anchor=north east] at (rel axis cs:0.975,0.975) {B};
\end{axis}
\end{tikzpicture}
\end{document}