Pgfplots:用置信区间图上的标签替换 x 轴编号?

Pgfplots:用置信区间图上的标签替换 x 轴编号?

尽管我尽力了,但似乎无法用(任意)标签替换 x 轴上的编号。

这是一半一半的图,其中的图是在 PGPPlots 中绘制的,我添加了两个 x 刻度来显示我希望它看起来的样子:我看过一些与条形图相关的示例,但我似乎无法将其转移到这个示例中。一如既往,感谢您的考虑。

更新:已将其与比例和宽度结合使用。谢谢大家。

梅威瑟:

\documentclass{article}
\usepackage{tikz}
\usepackage{rotating}
\usepackage{verbatim}
\usepackage{pgfplots}
\begin{document}
\begin{sidewaysfigure}
\begin{tikzpicture}[scale=1.1],
\centering
\begin{axis}[
height=8cm,
width=15cm,  
  ymax=5,
  ymin=-3,
  xmin=0.5,
  xmax=10.5,
  axis y line*=left,
  axis x line*=bottom]
\addplot+[only marks][error bars/.cd,y dir=both, y explicit]
coordinates {
(10,0.23333) +- (0.58257,-0.1159)
(9,1.4) +- (2.04003,0.75997)
(8,1.1) +- (1.82293,0.37707)
(7,1.3667) +- (2.0352,0.6981)
(6,0.26667) +- (0.94558,-0.41225)
(5,0) +- (0.59648,-0.59648)
(4,0.9) +- (1.67431,0.12569)
(3,-0.66667) +- (0.09502,-1.42835)
(2,0.56667) +- (1.30495,-0.17162)
(1,0.4) +- (1.06941,-0.26941)
};
\addplot[dashed] coordinates {(-1,0) (42,0)};
\end{axis}
\end{tikzpicture}%
}
\end{sidewaysfigure}
\end{document}

答案1

您可以使用任何符号坐标:(事实上,坐标本身可以是 A、B、C、D 等。)但对于您的解决方案,您只需要映射标签xtick。根据 Jakes 的评论,我还添加了xtick={1,...,10}将刻度明确映射到标签。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{rotating}

\begin{document}
\documentclass{article}
\usepackage{tikz}
\usepackage{rotating}
\usepackage{verbatim}
\usepackage{pgfplots}
\begin{document}
\begin{sidewaysfigure}
\begin{tikzpicture}[scale=1.1],
\centering
\begin{axis}[
height=8cm,
width=15cm,  
  ymax=5,
  ymin=-3,
  xmin=0.5,
  xmax=10.5,
  axis y line*=left,
  axis x line*=bottom,
  xticklabels={Duck happy,Cat happy,Octopus happy,Monkey happy, Human happy,Duck sad,Cat sad,Octopus sad,Monkey sad, Human sad},xtick={1,...,10},
  x tick label style={rotate=90,anchor=east}]
\addplot+[only marks][error bars/.cd,y dir=both, y explicit]
coordinates {
(10,0.23333) +- (0.58257,-0.1159)
(9,1.4) +- (2.04003,0.75997)
(8,1.1) +- (1.82293,0.37707)
(7,1.3667) +- (2.0352,0.6981)
(6,0.26667) +- (0.94558,-0.41225)
(5,0) +- (0.59648,-0.59648)
(4,0.9) +- (1.67431,0.12569)
(3,-0.66667) +- (0.09502,-1.42835)
(2,0.56667) +- (1.30495,-0.17162)
(1,0.4) +- (1.06941,-0.26941)
};
\addplot[dashed] coordinates {(0,0) (10.5,0)};
\end{axis}
\end{tikzpicture}%

\end{sidewaysfigure}
\end{document}

代码输出

答案2

这种方法非常接近。但它并不完美,因为零X线只连接第一个和最后一个X值,因此没有传递整个情节。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{rotating}

\begin{document}
  \begin{sidewaysfigure}
    \centering
    \begin{tikzpicture}[scale=1.1]
      \begin{axis}[%
        height=8cm,
        width=15cm,  
%        xmin=0.5,
%        xmax=10.5,
        ymax=5,
        ymin=-3,
        axis y line*=left,
        axis x line*=bottom,
                symbolic x coords={A,B,C,D,E,F,G,H,I,J},
        x tick label style={rotate=90,anchor=east},
                xticklabel={Picture~\tick}
      ]
        \addplot+[only marks][error bars/.cd,y dir=both,y explicit] coordinates {%
          (J,0.23333) +- (0.58257,-0.1159)
          (I,1.4) +- (2.04003,0.75997)
          (H,1.1) +- (1.82293,0.37707)
          (G,1.3667) +- (2.0352,0.6981)
          (F,0.26667) +- (0.94558,-0.41225)
          (E,0) +- (0.59648,-0.59648)
          (D,0.9) +- (1.67431,0.12569)
          (C,-0.66667) +- (0.09502,-1.42835)
          (B,0.56667) +- (1.30495,-0.17162)
          (A,0.4) +- (1.06941,-0.26941)
        };
        \addplot[dashed] coordinates {(A,0) (J,0)};
      \end{axis}
    \end{tikzpicture}%
  \end{sidewaysfigure}
\end{document}

在此处输入图片描述

答案3

我的答案基于 Alan Munn 的答案,但其中有一些缺陷(请参阅我在那里的评论)。还请注意,您给出的数字不是误差线顶部和底部的坐标,而是误差线的长度

\documentclass{article}
\usepackage{pgfplots}
\usepackage{rotating}

\begin{document}
\documentclass{article}
\usepackage{tikz}
\usepackage{rotating}
\usepackage{verbatim}
\usepackage{pgfplots}
\begin{document}
\begin{sidewaysfigure}
\begin{tikzpicture}[scale=1.1],
\centering
\begin{axis}[
height=8cm,
width=15cm,  
  ymax=5,
  ymin=-3,
  xmin=0.5,
  xmax=10.5,
  axis y line*=left,
  axis x line*=bottom,
  xticklabels={Duck happy,Cat happy,Octopus happy,Monkey happy, Human happy,Duck sad,Cat sad,Octopus sad,Monkey sad, Human sad},xtick={1,...,10},
  x tick label style={rotate=90,anchor=east}]
\addplot+[only marks][error bars/.cd,y dir=both, y explicit]
coordinates {
(1,0.4) -= (0,0.66941) += (0,0.66941)
(2,0.56667) -= (0,0.73829) += (0,0.73829)
(3,-0.66667) -= (0,0.76169) += (0,0.76169)
% etcetera
};
\addplot[dashed] coordinates {(0,0) (10.5,0)};
\end{axis}
\end{tikzpicture}%

\end{sidewaysfigure}
\end{document}

由于您想要的误差线是对称的,您也可以写:

(1,0.4) +- (0,0.66941)
(2,0.56667) +- (0,0.73829)
(3,-0.66667) +- (0,0.76169)

相关内容