Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to have this effect:

  1. The first set of xtick labels are exactly the first column in this table (is there such a command?);
  2. Then, under this first set of label, there should be another xtick labels, which are exactly the nth column in this table;

As the code sample shown below, I want to plot x1 vs y1. But I also want to use x2 as labels. Anyone knows how to do that? Thanks

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
height=8cm,
width=12cm,
grid=major,
legend pos=north west]
\addplot table[x=x1,y=y1] {
x1 y1 x2
16  4.27    3.24
32  1.84    1.96
64  1.82    1.57
80  2.06    1.51
128 1.29    2.51
160 2   2.98
256 2.45    5.41
320 2.71    20.78
}; 
\end{axis}
\end{tikzpicture}
\end{figure}

Edit:

I don't want the second set of labels look clustered, they should be just labels. I do NOT want to plot x2 vs y1, x2 is just for an additional set of labels. Each number should be right under the number from the first column. See the picture below, enter image description here

share|improve this question
    
Not sure if the answers provide what you want, I interpret it as you want each x2 label to be aligned below the corresponding x1 label. In that case you might be interested in the xticklabel option that allows you to use an expression that generates the label. –  hugovdberg Mar 31 at 12:21
    
No, I'll try to take a picture of what I really want. –  Daniel Mar 31 at 14:55
    
Well that's a completely different question now isn't it? –  percusse Mar 31 at 15:05
    
I deeply apologize for any confusion caused by my original post... –  Daniel Mar 31 at 15:08
    
@Daniel Please, have no worries. But demands and requests are of all kind these days, so when I am trying to help I try to be precise no matter what... See my edit. –  Malipivo Mar 31 at 16:07

2 Answers 2

Here is another approach by using extra x tick labels. You need to flatten the column you wanted from your table to an array via home-brewed \flattencolumn command then you can use the list as extra x ticks.

I have used a logarithmic axis since the data you want to have ticks with is too close to each other. So aesthetically I wouldn't do this.

\documentclass[]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\def\flattencolumn#1#2{
  \let\mycollist=\relax
  \pgfplotstableforeachcolumnelement{#2}\of#1\as\myentry{%
      \csname @ifundefined\endcsname{mycollist}{
        \edef\mycollist{\myentry}
      }{
        \edef\mycollist{\mycollist,\myentry}
      }
  }
}

\begin{document}

\pgfplotstableread{
x1 y1 x2
16  4.27    3.24
32  1.84    1.96
64  1.82    1.57
80  2.06    1.51
128 1.29    2.51
160 2       2.98
256 2.45    5.41
320 2.71    20.78
}\mytable

\begin{tikzpicture}
\flattencolumn{\mytable}{x2}
\begin{semilogxaxis}[
height=8cm,
width=15cm,
grid=major,
xtick=data,
xmin=1,
extra x ticks/.expanded={\mycollist},
extra x tick style={grid=major,
                    tick label style={
                      yshift=-5mm}
                    },
log ticks with fixed point,
legend pos=north west]
\addplot table[x=x1,y=y1] \mytable; 
\end{semilogxaxis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

I use rather dirty trick, but it works for me perfectly. I am layering the same graph with different settings but at the same time I am preserving the key values of the graph layout (xmin, xmax etc.). The disadvantage is that I am repeating loading of the same data several times, but the advantage is that I have full control over the output.

%! *latex mal-ticks.tex
\documentclass[border=5]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
\def\myset{xmin=0, xmax=350, ymin=1, ymax=4.5, 
height=10cm, width=18cm, legend pos=north west}
\def\nochange{only marks, no markers}

\begin{tikzpicture}
% Graf number 1: Labels with x=x1...
\begin{axis}[\myset, %extra x ticks=data,
  \nochange, xtick=data, grid=major, blue, x tick label style={yshift=-6mm}, hide y axis,
  ]
\addplot table[x=x1, y=y1] {
x1 y1 x2
16  4.27    3.24
32  1.84    1.96
64  1.82    1.57
80  2.06    1.51
128 1.29    2.51
160 2   2.98
256 2.45    5.41
320 2.71    20.78
};
\end{axis}

% Graph number 2: Labels with x=x2...
\begin{axis}[\myset, %extra x ticks=data,
  xtick=data, xticklabels={3.24, 1.96, 1.57, 1.51, 2.51, 2.98, 5.41, 20.78}, ytick=data, only marks, mark=x, mark options={magenta}, grid=both, x tick label style={yshift=-11mm}, x tick label style={magenta}, %hide y axis,
yticklabel style={/pgf/number format/zerofill},
  ]
\addplot table[x=x1, y=y1] {
x1 y1 x2
16  4.27    3.24
32  1.84    1.96
64  1.82    1.57
80  2.06    1.51
128 1.29    2.51
160 2   2.98
256 2.45    5.41
320 2.71    20.78
};
\end{axis}

% Graph number 3: The actual plotting...
\begin{axis}[\myset, %extra x ticks=data,
  minor xtick=data, x tick label style={yshift=-1mm}, hide y axis, %ymajorgrids,
  ]
\addplot table[x=x1, y=y1] {
x1 y1 x2
16  4.27    3.24
32  1.84    1.96
64  1.82    1.57
80  2.06    1.51
128 1.29    2.51
160 2   2.98
256 2.45    5.41
320 2.71    20.78
};
\end{axis}
\end{tikzpicture}

\end{document}

mwe of layering the same graph

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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