12

Try as I might, I can't seem to replace the numbering on the x-axis with (arbitrary) labels.

Here is a half-and-half, where the plot has been done in PGPPlots and I've added two of the x-ticks showing how I'd like it to look: I've seen a few examples in relation to barplots, but I can't seem to transfer it to this example. As always, thanks for the consideration.

Update: Got it working with a combination of scale and width. Thanks all.

MWE:

\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}
15

You can use anything as symbolic coordinates: (In fact, the coordinates themselves can be just A,B,C, D etc.) But for your solution, you just need to map the xtick labels. Based on Jakes comment, I've also added xtick={1,...,10} to explicitly map the ticks to the labels.

\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}

output of code

  • Let me have a work with it all. My REAL example has 42 labels...phew...Thanks all. – Frank Zafka Jun 3 '11 at 14:20
  • The solution should be the same. (The method is essentially the same as is used in R). The only odd think to watch out for is the two initial commas in the xtick labels list. – Alan Munn Jun 3 '11 at 14:24
  • @RSoul, @Alan: If you add the line xtick={1,...,10}, (or xtick={1,...,42}, in you real application) to the axis options, you can get rid of the two initial commas. If you don't specify the ticks explicitly, PGFplots will map the tick labels to ticks that aren't necessarily shown in the plot. – Jake Jun 3 '11 at 22:04
  • There are some severe problems with this answer. First of all, the data are not matched to the proper label (but rather randomly, it seems). This is mysteriously fixed by putting them in the right order. Secondly, you are using symmetric error bars; the x in +- (x,y) gives the horizontal error bar size, which is ignored as horizontal error bars are not enabled. See my answer. – dremodaris May 3 '15 at 15:02
4

This approach comes pretty close. But it is not perfect since the zero x line only connects the first and last x value, thus is not passing the whole plot.

\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}

enter image description here

  • Donig Thanks for that! However, I'm not sure if this has got me a lot further along. The labels I am using are not as simple as Picture A, Picture B, but more like "Reindeer 1", "Octopus 2", "Human Face". Your way is neat, but I'm still at a loss to use arbitrary labels. – Frank Zafka Jun 3 '11 at 12:29
  • @Christoph I have added some labels above. I can have them in whatever format is needed. I can't see how to replace the numbers with the labels though--that's where I'm stuck! – Frank Zafka Jun 3 '11 at 13:39
1

My answer is based on Alan Munn's, which has a few flaws (see my comment there). Note also that the numbers you give are not the coordinates of the top and bottom of the error bars, but rather the error bars' length.

\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}

As your intended error bars turn out to be symmetric, you could as well write:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.