如何堆叠和对齐具有相同 x 但不同 y 轴的多个图?

如何堆叠和对齐具有相同 x 但不同 y 轴的多个图?

我有来自多个站点的多个数据点系列。我想生成一系列堆叠图,像附件示例中那样紧密排列,以便进行系列内和系列间比较。我使用 pgfplots 的“groupplot”功能生成了示例,但它不允许我为每个变量设置不同的 y 轴。

pgf 手册说您还可以在矩阵中对多个图进行分组(这将允许不同的 y 轴),但在这种情况下,我怎样才能让这些图像本例中一样“粘在一起”?

这是我的示例代码:

% Preview source code

%% LyX 2.0.1 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.

\usepackage {tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\makeatother

\usepackage{babel}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group name=my plots,group size=1 by 3,xlabels at=edge bottom,xticklabels at=edge bottom,ylabels at=edge left,yticklabels at=edge left,vertical sep=0pt},footnotesize,width=8cm,height=3cm,xlabel=Site ,,xmin=-0.5,xmax=10.5,ymin=0,ymax=30,ytick={0,10,...,20},xtick={0,1,...,12},xticklabels={A,B,C,D,E,F,G,H,I,J,K},tickpos=left,ytick align=outside,xtick align=outside]
\nextgroupplot
\addplot [ybar,fill=blue]coordinates{(0,0) (1,9.94) (2,0.72)(3,1.17)(4,1.05)(5,0.67)(6,0.24)(7,1.75)(8,0)(9,0)(10,1.21)(11,0)};
\nextgroupplot
\addplot [ybar,fill=red]coordinates{(0,0) (1,14.04) (2,0.72)(3,0)(4,22.81)(5,3.34)(6,2.05)(7,11.23)(8,2.59)(9,0)(10,0.61)(11,0)};
\nextgroupplot
\addplot [ybar,fill=red]coordinates{(0,0) (1,0) (2,0)(3,0)(4,0.53)(5,0.33)(6,0.73)(7,0)(8,0)(9,0)(10,3.9)(11,0.17)};
\end{groupplot}
\end{tikzpicture}

\end{document}

堆叠图示例

答案1

groupplot您可以通过将选项传递给以下选项来调整环境中各个图的轴选项\nextgroupplot

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group name=my plots,
        group size=1 by 3,
        xlabels at=edge bottom,
        xticklabels at=edge bottom,
        vertical sep=0pt
    },
    ybar,
    footnotesize,
    width=8cm,
    height=3cm,
    xlabel=Site,
    xmin=-0.5, xmax=10.5,
    ymin=0, ymax=29,
    xtick={0,1,...,12},
    xticklabels={A,B,C,D,E,F,G,H,I,J,K},
    tickpos=left,
    ytick align=outside,
    xtick align=outside
]
\nextgroupplot[ymax=11]
\addplot [fill=blue] coordinates{(0,0) (1,9.94) (2,0.72)(3,1.17)(4,1.05)(5,0.67)(6,0.24)(7,1.75)(8,0)(9,0)(10,1.21)(11,0)};
\nextgroupplot
\addplot [fill=red] coordinates{(0,0) (1,14.04) (2,0.72)(3,0)(4,22.81)(5,3.34)(6,2.05)(7,11.23)(8,2.59)(9,0)(10,0.61)(11,0)};
\nextgroupplot[ymax=5]
\addplot [fill=red] coordinates{(0,0) (1,0) (2,0)(3,0)(4,0.53)(5,0.33)(6,0.73)(7,0)(8,0)(9,0)(10,3.9)(11,0.17)};
\end{groupplot}
\end{tikzpicture}

\end{document}

答案2

您可以简单地使用三个单独的tikzpicture环境并根据需要指定不同的 y 轴值。为了便于比较图表,我建议添加y=0.05cm以固定 y 单位向量长度。这会导致三个图彼此重叠,但它们之间有垂直空间。要删除这个垂直空间,我们需要将边界框设置为轴框架,\pgfresetboundingbox然后使用:

\useasboundingbox (current axis.south west) rectangle (current axis.north east);

这使得图之间的垂直间距几乎为零,但并没有完全消除。因此,为了解决这个问题,我添加了一个(0,0.3ex)模糊因子,如下面的代码所示。可能有更好的方法来设置它,但这样会产生所需的结果:

在此处输入图片描述

\documentclass{article}

\usepackage {tikz}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\pgfkeys{/pgfplots/my group style/.style={
    group style={group name=my plots,group size=1 by 4,xlabels at=edge bottom,
        xticklabels at=edge bottom,ylabels at=edge left,yticklabels at=edge left,
        vertical sep=0pt},
    footnotesize,width=8cm,height=3cm,xlabel=Site, xmin=-0.5, xmax=10.5, ymin=0, ymax=30, 
    ytick={0,10,...,30}, xtick={1,2,...,12}, xticklabels={A,B,C,D,E,F,G,H,I,J,K}, 
    tickpos=left, ytick align=outside, xtick align=outside,
    y=0.05cm% Fixed y unit vector to allow for comparrison
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[my group style,xlabel={},xticklabels={},ymax=25]
\addplot [ybar,fill=blue]coordinates{(0,0) (1,9.94) (2,0.72)(3,1.17)(4,1.05)(5,0.67)(6,0.24)(7,1.75)(8,0)(9,0)(10,1.21)(11,0)};
\end{axis}
\pgfresetboundingbox
\useasboundingbox ($(current axis.south west)+(0,0.3ex)$)
    rectangle (current axis.north east);
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[my group style,xlabel={},xticklabels={},ymax=35]
\addplot [ybar,fill=red]coordinates{(0,0) (1,14.04) (2,0.72)(3,0)(4,22.81)(5,3.34)(6,2.05)(7,11.23)(8,2.59)(9,0)(10,0.61)(11,0)};
\end{axis}
\pgfresetboundingbox
\useasboundingbox ($(current axis.south west)+(0,0.3ex)$)
    rectangle (current axis.north east);
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[my group style,ymax=25]
\addplot [ybar,fill=red]coordinates{(0,0) (1,0) (2,0)(3,0)(4,0.53)(5,0.33)(6,0.73)(7,0)(8,0)(9,0)(10,3.9)(11,0.17)};
\end{axis}
\pgfresetboundingbox
\useasboundingbox ($(current axis.south west)+(0,0.3ex)$)
    rectangle (current axis.north east);
\end{tikzpicture}
\end{document}

相关内容