我被 pgfplot 中的花括号/大括号问题困扰了一段时间。
我想要 x 轴下方的花括号中包含一些文本(例如第 1 部分、第 2 部分等):
我不知道下一步该怎么做。手册没有解释清楚。下面是 MWE:
\documentclass{report}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{pgfkeys}
\usepackage{subcaption}
\pgfplotsset{compat=newest}
\usepackage{pgfmath}
\pgfplotsset{plot coordinates/math parser=false}
\begin{document}
\begin{figure}[H]
\centering
\pgfplotstableread{
x y y-max y-min
1 0.067783 0.02 0.02
2 0.066945 0.01 0.02
3 0.043508 0.02 0.02
4 0.043508 0.01 0.02
5 0.043508 0.01 0.02
6 0.043508 0.01 0.02
7 0.043508 0.02 0.02
8 0.043508 0.02 0.02
9 0.043508 0.01 0.01
10 0.043508 0.02 0.02
11 0.043508 0.01 0.01
12 0.043508 0.02 0.02
13 0.043508 0.01 0.01
14 0.043508 0.02 0.02
15 0.043508 0.01 0.01
16 0.043508 0.01 0.02
17 0.043508 0.02 0.01
18 0.043508 0.01 0.01
19 0.034655 0.02 0.02
20 0.051180 0.01 0.01
21 0.062882 0.02 0.02
}{\mytable}
\centering
\begin{tikzpicture}
\begin{axis}[
height=65mm,
width= \textwidth,
ymin=0,
ylabel={Value},
xlabel= {Coordinate},
symbolic x coords={1,2,3, ,4,5,6,7,8,9,10,11,12,13,14,15, ,16,17,18,19,20,21},
xticklabels={23,24,25,6,7,8,9,11,12,13,14,15,16,26,27, 17,18,19,20,21,22},
xtick=data,
/pgf/number format/fixed,
/pgf/number format/precision=2,
]
\addplot [only marks]
plot [error bars/.cd, y dir=both, y explicit, error bar style={line width=1pt}, error mark options={
rotate=90,
mark size=2pt,
line width=1pt
}]
table [y error plus=y-max, y error minus=y-min] {\mytable};
\addplot[red, only marks, mark =diamond, mark size=2.5, thick]
coordinates {(1,0.0451648) (2,0.1095861) (3,0.1432419)
(4,0.1648329) (5,0.1649211051) (6,0.1517038807) (7,0.11349211051) (8,0.1249211051) (9,0.17549211051) (10,0.12949211051) (11,0.149211051) (12,0.16749211051) (13,0.19749211051) (14,0.1211051) (15,0.19211051) (16,0.13249211051) (17,0.16349211051) (18,0.19649211051) (19,0.10149211051) (20,0.12149211051) (21,0.134249211051) };
\addplot[blue, only marks, mark =star, mark size=3, thick]
coordinates {(1,0.051648) (2,0.095861) (3,0.132419)
(4,0.148329) (5,0.149211051) (6,0.117038807) (7,0.1349211051) (8,0.149211051) (9,0.1549211051) (10,0.1949211051) (11,0.19211051) (12,0.1649211051) (13,0.1749211051) (14,0.19211051) (15,0.149211051) (16,0.1249211051) (17,0.1349211051) (18,0.1649211051) (19,0.11049211051) (20,0.1149211051) (21,0.1249211051) };
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
我使用xticklabel style
键来命名每个 x 刻度标签节点。装饰库可以绘制括号而不是线条。
虽然有一个用于调整振幅的键,但它并不比宏更容易使用。
\documentclass{report}
\usepackage{pgfplots}
\usetikzlibrary{decorations}
\usepackage{float}
\usepackage{pgfkeys}
\usepackage{subcaption}
\pgfplotsset{compat=newest}
\usepackage{pgfmath}
\pgfplotsset{plot coordinates/math parser=false}
\begin{document}
\begin{figure}[H]
\centering
\pgfplotstableread{
x y y-max y-min
1 0.067783 0.02 0.02
2 0.066945 0.01 0.02
3 0.043508 0.02 0.02
4 0.043508 0.01 0.02
5 0.043508 0.01 0.02
6 0.043508 0.01 0.02
7 0.043508 0.02 0.02
8 0.043508 0.02 0.02
9 0.043508 0.01 0.01
10 0.043508 0.02 0.02
11 0.043508 0.01 0.01
12 0.043508 0.02 0.02
13 0.043508 0.01 0.01
14 0.043508 0.02 0.02
15 0.043508 0.01 0.01
16 0.043508 0.01 0.02
17 0.043508 0.02 0.01
18 0.043508 0.01 0.01
19 0.034655 0.02 0.02
20 0.051180 0.01 0.01
21 0.062882 0.02 0.02
}{\mytable}
\centering
\begin{tikzpicture}
\begin{axis}[
height=65mm,
width= \textwidth,
ymin=0,
ylabel={Value},
xlabel= {Coordinate},
xlabel style={below=20pt},
symbolic x coords={1,2,3, ,4,5,6,7,8,9,10,11,12,13,14,15, ,16,17,18,19,20,21},
xticklabels={23,24,25,6,7,8,9,11,12,13,14,15,16,26,27, 17,18,19,20,21,22},
xtick=data,
/pgf/number format/fixed,
/pgf/number format/precision=2,
xticklabel style={name=T\ticknum}% names every xtick label node T0,T1, ...
]
\addplot [only marks]
plot [error bars/.cd, y dir=both, y explicit, error bar style={line width=1pt}, error mark options={
rotate=90,
mark size=2pt,
line width=1pt
}]
table [y error plus=y-max, y error minus=y-min] {\mytable};
\addplot[red, only marks, mark =diamond, mark size=2.5, thick]
coordinates {(1,0.0451648) (2,0.1095861) (3,0.1432419)
(4,0.1648329) (5,0.1649211051) (6,0.1517038807) (7,0.11349211051) (8,0.1249211051) (9,0.17549211051) (10,0.12949211051) (11,0.149211051) (12,0.16749211051) (13,0.19749211051) (14,0.1211051) (15,0.19211051) (16,0.13249211051) (17,0.16349211051) (18,0.19649211051) (19,0.10149211051) (20,0.12149211051) (21,0.134249211051) };
\addplot[blue, only marks, mark =star, mark size=3, thick]
coordinates {(1,0.051648) (2,0.095861) (3,0.132419)
(4,0.148329) (5,0.149211051) (6,0.117038807) (7,0.1349211051) (8,0.149211051) (9,0.1549211051) (10,0.1949211051) (11,0.19211051) (12,0.1649211051) (13,0.1749211051) (14,0.19211051) (15,0.149211051) (16,0.1249211051) (17,0.1349211051) (18,0.1649211051) (19,0.11049211051) (20,0.1149211051) (21,0.1249211051) };
\end{axis}
\begin{scope}[decoration=brace]
\pgfdecorationsegmentamplitude=5pt
\draw[decorate] (T2.south east) -- (T0.south west) node[midway,below=\pgfdecorationsegmentamplitude] {Part 1};
\draw[decorate] (T14.south east) -- (T3.south west) node[midway,below=\pgfdecorationsegmentamplitude] {Part 2};
\draw[decorate] (T20.south east) -- (T15.south west) node[midway,below=\pgfdecorationsegmentamplitude] {Part 3};
\end{scope}
\end{tikzpicture}
\end{figure}
\end{document}