使用组图绘制双轴图

使用组图绘制双轴图

我正在尝试绘制主成分分析的输出。在本例中,我想将图分组,显示 PC1 vs PC2 和 PC1 vs PC3。

预期输出如下所示: 预期输出

对应的代码是:

\begin{tikzpicture}
%%%
%% PLOTSET 1
%%%
\begin{axis}[
    xlabel= PC1,
    ylabel= PC2,
    xmin=-3.0,xmax=3.0,
    ymin=-3.0, ymax=3.0,    
]
\end{axis}
\begin{axis}[
    legend columns=-1,
    legend style={
    draw=none,
    at={(0.5,-0.26)},
    anchor=center,
    /tikz/every even column/.append style={column sep=0.5cm}},
    yticklabels=\empty,
    xticklabels=\empty,
    xmin=-3.0,xmax=3.0,
    ymin=-3.0, ymax=3.0,
    axis lines=middle,
    axis line style={latex-latex, black, dashed},
    grid=major,
    grid style={dotted},
    scatter, only marks,
    scatter src=explicit symbolic,%
]
%%Insert datapoints here
\end{axis}
\end{tikzpicture}
%%%
%% PLOTSET 2
%%%
\begin{tikzpicture}
\begin{axis}[
    xlabel= PC1,
    ylabel= PC3,
    xmin=-3.0,xmax=3.0,
    ymin=-3.0, ymax=3.0,    
]
\end{axis}
%
\begin{axis}[
    legend columns=-1,
    legend style={
    draw=none,
    at={(0.5,-0.26)},
    anchor=center,
    /tikz/every even column/.append style={column sep=0.5cm}},
    yticklabels=\empty,
    xticklabels=\empty,
    xmin=-3.0,xmax=3.0,
    ymin=-3.0, ymax=3.0,
    axis lines=middle,
    axis line style={latex-latex, black, dashed},
    grid=major,
    grid style={dotted},
    scatter, only marks,
    scatter src=explicit symbolic,%
]
%%Insert datapoints here
\end{axis}
\end{tikzpicture}

为了使它能够扩展到将来可能需要添加的情况,比如说再添加一个图和一个箱线图,或者类似的东西,我希望用 重写此代码groupplot。当我尝试用\begin{axis}[]相应的替换 时\nextgroupplot[],我总是遇到错误。

答案1

我认为问题在于双轴环境,使用groupplotnextgroupplot没有办法做到这一点。如果我理解正确的话,我认为不可能将双图作为单个图重复。否则,如果您想使用groupplot,您可以从以下类似内容开始:

\documentclass{article}

\usepackage{tikz}

\usepackage{pgfplots}
\pgfplotsset{width= 7cm, compat=newest}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{tikzpicture}
\begin{groupplot}[ 
group style={group size=2 by 3, horizontal sep=50pt, vertical sep=50pt},
 xlabel= PC1,
ylabel= PC2,
xmin=-3.0,xmax=3.0,
ymin=-3.0, ymax=3.0,
grid=major,
grid style={dotted},
] 

\nextgroupplot
\draw [>=latex, <->, black, dashed] (-3,0) -- (3,0);
\draw [>=latex, <->, black, dashed] (0,-3) -- (0,3);

\nextgroupplot[ylabel=PC3]
\draw [>=latex, <->, black, dashed] (-3,0) -- (3,0);
\draw [>=latex, <->, black, dashed] (0,-3) -- (0,3);

\nextgroupplot
\draw [>=latex, <->, black, dashed] (-3,0) -- (3,0);
\draw [>=latex, <->, black, dashed] (0,-3) -- (0,3);

\nextgroupplot[ylabel=PC3]
\draw [>=latex, <->, black, dashed] (-3,0) -- (3,0);
\draw [>=latex, <->, black, dashed] (0,-3) -- (0,3);

\nextgroupplot
\draw [>=latex, <->, black, dashed] (-3,0) -- (3,0);
\draw [>=latex, <->, black, dashed] (0,-3) -- (0,3);

\nextgroupplot[ylabel=PC3]
\draw [>=latex, <->, black, dashed] (-3,0) -- (3,0);
\draw [>=latex, <->, black, dashed] (0,-3) -- (0,3);

\end{groupplot}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容