旧答案

旧答案

我有多个条形图,我想对齐它们,以便所有条形图都从同一点开始 - 我该如何实现这一点?

编辑:我忘了提的一件事是,中间还有其他元素。我相应地编辑了 MWE。也许是为了更清楚:它用于显示问卷的结果。因此格式是标题 -> 枚举列表 -> 标题 -> 图表 -> 标题 -> 枚举列表 -> 标题 -> 图表等等

我想要的是:

左边的红线是开始\textwidth,右边的红线表示对齐

我拥有的:

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{
   compat=newest,
   axispower/.style={
    xbar,
    y axis line style={ opacity=0 },
    axis x line=none,
    tickwidth=0pt,
    xmin=0,
    y=20pt,
    nodes near coords,
    bar shift=0pt,
    ytick=data
  }
}
\begin{document}

\subsubsection{Notification-System} \label{results1}
\begin{tikzpicture}
  \begin{axis}[axispower, symbolic y coords={Chat,E-Mail,SMS,Web}]
  \addplot coordinates {(68,Web)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\vspace{1cm}

\subsubsection{Notification-System 2} \label{results2}
\begin{tikzpicture}
  \begin{axis}[axispower, symbolic y coords={Chat,E-Mail,SMS,Web-Notification}]
  \addplot coordinates {(68,Web-Notification)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\end{document}

答案1

您可以更改标签的样式,使得它们的最小宽度都大于最大宽度。

 yticklabel style={text width=28mm,align=right},

截屏

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\newcommand{\axispower}{
    xbar,
    y axis line style={ opacity=0 },
    axis x line=none,
    yticklabel style={text width=28mm,align=right},
    tickwidth=0pt,
    xmin=0,
    y=20pt,
    nodes near coords,
    bar shift=0pt
}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[\axispower, symbolic y coords={Chat,E-Mail,SMS,Web,Web-Notification}]
  \addplot coordinates {(68,Web)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}
  \begin{axis}[\axispower, symbolic y coords={Chat,E-Mail,SMS,Web-Notification}]
  \addplot coordinates {(68,Web-Notification)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\end{document}

旧答案

你可以把它们放在同一个tikzpicture环境中。第二个axis环境低几厘米,这里是 3.5 厘米。

%\begin{tikzpicture}
  \begin{axis}[\axispower, symbolic y coords={Chat,E-Mail,SMS,Web-Notification},yshift=-3.5cm]

截屏

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\newcommand{\axispower}{
    xbar,
    y axis line style={ opacity=0 },
    axis x line=none,
    tickwidth=0pt,
    xmin=0,
    y=20pt,
    nodes near coords,
    bar shift=0pt
}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[\axispower, symbolic y coords={Chat,E-Mail,SMS,Web},local bounding box=premiere]
  \addplot coordinates {(68,Web)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
%\end{tikzpicture}
%
%\vspace{1cm}
%
%\begin{tikzpicture}
  \begin{axis}[\axispower, symbolic y coords={Chat,E-Mail,SMS,Web-Notification},yshift=-3.5cm]
  \addplot coordinates {(68,Web-Notification)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\end{document}

答案2

快速解决方法是使用

\hspace{3cm}\begin{tikzpicture}[trim axis left]

对于两个tikzpictures,trim axis left设置 的边界框tikzpicture,使其左边缘位于 的左边缘axis,不包括标签。水平空间用于确保标签不会进入左边距。

在此处输入图片描述

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{
   compat=newest,
   axispower/.style={
    xbar,
    y axis line style={ opacity=0 },
    axis x line=none,
    tickwidth=0pt,
    xmin=0,
    y=20pt,
    nodes near coords,
    bar shift=0pt,
  }
}
\begin{document}

\subsubsection{Notification-System} \label{results1}
\hspace{3cm}\begin{tikzpicture}[trim axis left]
  \begin{axis}[axispower, symbolic y coords={Chat,E-Mail,SMS,Web}]
  \addplot coordinates {(68,Web)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}


\subsubsection{Notification-System 2} \label{results2}
\hspace{3cm}\begin{tikzpicture}[trim axis left]
  \begin{axis}[axispower, symbolic y coords={Chat,E-Mail,SMS,Web-Notification}]
  \addplot coordinates {(68,Web-Notification)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\end{document}

旧答案

环境groupplot(来自pgfplotsgroupplots)适用于需要在网格中对齐多个轴的情况,这基本上就是您在此处所做的。它相当简单,使用环境groupplot而不是axis,使用设置网格中的行数和列数group style={group size=1 by 2}(在这里您还可以设置轴之间的空间和其他内容),axis然后添加常用选项。使用开始每个新轴\nextgroupplot[<local settings for this axis>]

在下面的代码中,我还创建了axispower一种样式而不是宏,我认为这更有意义。

\documentclass{article} 
\usepackage{pgfplots,showframe}
\usepgfplotslibrary{groupplots}
\pgfplotsset{
   compat=newest,
   axispower/.style={
    xbar,
    y axis line style={ opacity=0 },
    axis x line=none,
    tickwidth=0pt,
    xmin=0,
    y=20pt,
    nodes near coords,
    bar shift=0pt,
  }
}
\begin{document}

\begin{tikzpicture}
  \begin{groupplot}[
     group style={group size=1 by 2},
     axispower
]

\nextgroupplot[symbolic y coords={Chat,E-Mail,SMS,Web}]
  \addplot coordinates {(68,Web)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};

\nextgroupplot[symbolic y coords={Chat,E-Mail,SMS,Web-Notification}]
  \addplot coordinates {(68,Web-Notification)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{groupplot}
\end{tikzpicture}

\end{document}

截图中的框架来自showframe包,表示文本块。

在此处输入图片描述

答案3

另一个可能的解决方案:将两者放在axis一个tikzpicture环境中并使用anchor选项来对齐它们。

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\newcommand{\axispower}{
    xbar,
    y axis line style={ opacity=0 },
    axis x line=none,
    tickwidth=0pt,
    xmin=0,
    y=20pt,
    nodes near coords,
    bar shift=0pt
}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
  [\axispower,
   symbolic y coords={Chat,E-Mail,SMS,Web},
   name=top plot,]
  \addplot coordinates {(68,Web)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
%\end{tikzpicture}

%\vspace{1cm}

%\begin{tikzpicture}
  \begin{axis}[
  \axispower, 
  symbolic y coords={Chat,E-Mail,SMS,Web-Notification},
  anchor=north west,
  at={(top plot.below south west)},
  yshift=-5mm]
  \addplot coordinates {(68,Web-Notification)};
  \addplot coordinates {(22,SMS)};
  \addplot coordinates {(50,E-Mail)};
  \addplot coordinates {(52,Chat)};
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容