\groupplot 中的绘图如何水平和垂直移动?

\groupplot 中的绘图如何水平和垂直移动?

这是我的代码:

\documentclass[tikz,12pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{mathptmx}    
\usetikzlibrary{pgfplots.groupplots}
\usepackage{pgfplots}
\pgfplotsset{width=12cm,compat=1.9}

\usepackage{filecontents}
\begin{filecontents}{1.csv}
400 0.02
450 0.03
500 0.035
550 0.08
600 0.1
650 0.05
700 0.15
750 0.12
800 0.02
850 0.05
900 0.1
\end{filecontents}
\begin{filecontents}{2.csv}
1   0.02
2   0.03
3 0.035
4   0.08
5   0.1
6 0.05
7   0.15
8 0.12
9   0.02
10  0.05
11  0.1
\end{filecontents}

\begin{document}
\pgfplotsset{every axis/.append style={
line width=.5 pt,
tick style={line width=.6pt}}}
\begin{tikzpicture}
\begin{groupplot}[group style={
        group size=2 by 1,
        vertical sep=-10cm,
        horizontal sep=-8.5cm,
    },]    
\nextgroupplot[
height=10cm,
xmin=400,
ymax=0.2,
enlarge x limits=.0,
enlarge y limits=.02,
ylabel={Absorption, A.U.},
xlabel={Wavelength, nm},
minor x tick num=4,
minor y tick num=4,
tick label style={/pgf/number format/fixed},
%xtick={200, 220, ...,360},
legend style={legend pos=north west,legend cell align=left,font=\tiny,draw=none,text height=0.1em},
]

\addplot[green] table [x index=0,y index=1] {1.csv};

\nextgroupplot[
font=\tiny,
height=5cm,
width=4cm,
ymax=0.12,
enlarge x limits=.0,
enlarge y limits=.02,
ylabel={Absorption at 740\,nm, A.U.},
xlabel={pH},
x label style={at={(axis description cs:0.5,-.05)},anchor=north},
y label style={at={(axis description cs:-0.2,.5)},anchor=south},
tick label style={/pgf/number format/fixed},
minor x tick num=4,
minor y tick num=4,
%xtick={200, 220, ...,360},
legend style={legend pos=north west,legend cell align=left,font=\tiny,draw=none,text height=0.1em},
]
\addplot[black,mark=*,dashed,line width=0.5pt] table [x index=0,y index=1] {2.csv};
\end{groupplot}
\end{tikzpicture}

\end{document}

这就是我所拥有的:

在此处输入图片描述

我想将子图移到上面。该怎么做?我应该使用groupplot还是有其他方法可以完成我想做的事情?

答案1

在我看来,groupplots并不是真正为这种事情而设计的。我建议改用两个axis环境。使用或坐标系将一个放置\coordinate在较大的轴上,并将较小的轴放置在这个坐标上。rel axis csaxis cs

在下面的代码中你会看到

\coordinate (otheraxis) at (rel axis cs:0.15,0.5);

在第一个轴中axis,在第二个轴的选项中

at={(otheraxis)},

axis将的左下角置于otheraxis。这样,您可以通过修改坐标轻松移动较小的轴otheraxis

\documentclass[tikz,12pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{mathptmx}    
\usetikzlibrary{pgfplots.groupplots}
\usepackage{pgfplots}
\pgfplotsset{width=12cm,compat=1.9}

\usepackage{filecontents}
\begin{filecontents}{1.csv}
400 0.02
450 0.03
500 0.035
550 0.08
600 0.1
650 0.05
700 0.15
750 0.12
800 0.02
850 0.05
900 0.1
\end{filecontents}
\begin{filecontents}{2.csv}
1   0.02
2   0.03
3 0.035
4   0.08
5   0.1
6 0.05
7   0.15
8 0.12
9   0.02
10  0.05
11  0.1
\end{filecontents}

\begin{document}
\pgfplotsset{every axis/.append style={
line width=.5 pt,
tick style={line width=.6pt}}}

\begin{tikzpicture}
\begin{axis}[
height=10cm,
xmin=400,
ymax=0.2,
enlarge x limits=.0,
enlarge y limits=.02,
ylabel={Absorption, A.U.},
xlabel={Wavelength, nm},
minor x tick num=4,
minor y tick num=4,
tick label style={/pgf/number format/fixed},
%xtick={200, 220, ...,360},
legend style={legend pos=north west,legend cell align=left,font=\tiny,draw=none,text height=0.1em},
]

\addplot[green] table [x index=0,y index=1] {1.csv};

\coordinate (otheraxis) at (rel axis cs:0.15,0.5);
\fill [red] (otheraxis) circle[radius=3pt];
\end{axis}

\begin{axis}[
at={(otheraxis)},
font=\tiny,
height=5cm,
width=4cm,
ymax=0.12,
enlarge x limits=.0,
enlarge y limits=.02,
ylabel={Absorption at 740\,nm, A.U.},
xlabel={pH},
x label style={at={(axis description cs:0.5,-.05)},anchor=north},
y label style={at={(axis description cs:-0.2,.5)},anchor=south},
tick label style={/pgf/number format/fixed},
minor x tick num=4,
minor y tick num=4,
%xtick={200, 220, ...,360},
legend style={legend pos=north west,legend cell align=left,font=\tiny,draw=none,text height=0.1em},
]
\addplot[black,mark=*,dashed,line width=0.5pt] table [x index=0,y index=1] {2.csv};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容