# Set the secondary x axis limits the same as the first one on a data plot

I want to plot a data with two x axis (top and bottom) and a y axis. The first x and y axes are set from data. The second x axis limits should be same as the first one. I also need to label some ticks on the second x axis. Pgfplots doesn't allow me to label (see figure).

How can I achieve the results?

\documentclass[12pt,a4paper]{article}%
\usepackage{pgfplots}
\usepackage{filecontents}
\def\w{11}
\def\h{10}

\begin{filecontents*}{mydata.csv}
0,0,0
0.5963459,1.402948075,0
1.1520177,2.71020725,0
1.6688749,3.9261522,0
2.148838675,5.055302625,0
2.593862225,6.1022536,0
3.0059051,7.0716151,0
3.386911525,7.967961025,0
3.854113525,8.763110075,0.30397835
4.7655312,9.359200525,1.8520657
5.6033659,9.907176575,3.27515805
6.73565105,10.2090156,4.570585925
7.803365275,10.46821083,5.760149325
8.782843025,10.70411198,6.852623
9.6808764,10.91879745,7.855273375
10.50381463,11.11416708,8.7749295
11.25757995,11.29195553,9.617991625
11.91476308,11.35993613,10.04455928
12.51314918,11.4149591,10.41695778
24.97249018,14.50643975,15.46234698
24.97679915,14.50785408,15.4627031
24.98072545,14.5091428,15.46302755
24.984303,14.51031708,15.46332318
24.9875628,14.51138705,15.46359253
24.99053308,14.512362,15.46383795
24.99323953,14.51325038,15.46406155
24.99570558,14.51405983,15.46426528
24.99795258,14.51479738,15.46445093
25,14.51546943,15.46462005
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=\w cm, height=\h cm,
scale only axis,
axis x line*=bottom,
axis y line*=left,
table/x index={0},thick,
xlabel=$x$,
ylabel=$y$,
]
\foreach \ind in {1,2}{
\addplot table[y index={\ind}, col sep=comma, mark=none] {mydata.csv};
}
\end{axis}
%%%%%%%%%%
\begin{axis}[
width=\w cm, height=\h cm,
scale only axis,thick,
axis x line*=top,
axis y line*=right,
xtick={3,7},
xticklabels={a,b},
yticklabels=none
]
\end{axis}
\end{tikzpicture}

\end{document}

• If you add \addplot[opacity=0,domain=2.5:7.5] {x}; to the second axis, you will get a and b. – user121799 Jul 14 '18 at 11:10
• Thanks. How to set the top axis limits to the same as the bottom one automatically depending on the data? – mert Jul 14 '18 at 11:23
• I made an update. If you only want to draw one plot, you do not need to use two axis environments. – user121799 Jul 14 '18 at 11:50

UPDATE: If you really only want to add the a and b labels, you do not need all the two-axis trickery.

\documentclass[12pt,a4paper]{article}%
\usepackage{pgfplots}
\usepackage{filecontents}
\def\w{11}
\def\h{10}

\begin{filecontents*}{mydata.csv}
0,0,0
0.5963459,1.402948075,0
1.1520177,2.71020725,0
1.6688749,3.9261522,0
2.148838675,5.055302625,0
2.593862225,6.1022536,0
3.0059051,7.0716151,0
3.386911525,7.967961025,0
3.854113525,8.763110075,0.30397835
4.7655312,9.359200525,1.8520657
5.6033659,9.907176575,3.27515805
6.73565105,10.2090156,4.570585925
7.803365275,10.46821083,5.760149325
8.782843025,10.70411198,6.852623
9.6808764,10.91879745,7.855273375
10.50381463,11.11416708,8.7749295
11.25757995,11.29195553,9.617991625
11.91476308,11.35993613,10.04455928
12.51314918,11.4149591,10.41695778
24.97249018,14.50643975,15.46234698
24.97679915,14.50785408,15.4627031
24.98072545,14.5091428,15.46302755
24.984303,14.51031708,15.46332318
24.9875628,14.51138705,15.46359253
24.99053308,14.512362,15.46383795
24.99323953,14.51325038,15.46406155
24.99570558,14.51405983,15.46426528
24.99795258,14.51479738,15.46445093
25,14.51546943,15.46462005
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=\w cm, height=\h cm,
scale only axis,
table/x index={0},thick,
xlabel=$x$,
ylabel=$y$,
]
\foreach \ind in {1,2}{
\addplot table[y index={\ind}, col sep=comma, mark=none] {mydata.csv};
}
\coordinate (3) at (axis cs:3,0);
\coordinate (7) at (axis cs:7,0);
\end{axis}
\draw[thin] (3 |- current axis.north east) node[above] {a} -- ++(0,-4pt);
\draw[thin] (7 |- current axis.north east) node[above] {b} -- ++(0,-4pt);
%%%%%%%%%%
\end{tikzpicture}
\end{document}


\documentclass[12pt,a4paper]{article}%
\usepackage{pgfplots}
\usepackage{filecontents}
\def\w{11}
\def\h{10}

\begin{filecontents*}{mydata.csv}
0,0,0
0.5963459,1.402948075,0
1.1520177,2.71020725,0
1.6688749,3.9261522,0
2.148838675,5.055302625,0
2.593862225,6.1022536,0
3.0059051,7.0716151,0
3.386911525,7.967961025,0
3.854113525,8.763110075,0.30397835
4.7655312,9.359200525,1.8520657
5.6033659,9.907176575,3.27515805
6.73565105,10.2090156,4.570585925
7.803365275,10.46821083,5.760149325
8.782843025,10.70411198,6.852623
9.6808764,10.91879745,7.855273375
10.50381463,11.11416708,8.7749295
11.25757995,11.29195553,9.617991625
11.91476308,11.35993613,10.04455928
12.51314918,11.4149591,10.41695778
24.97249018,14.50643975,15.46234698
24.97679915,14.50785408,15.4627031
24.98072545,14.5091428,15.46302755
24.984303,14.51031708,15.46332318
24.9875628,14.51138705,15.46359253
24.99053308,14.512362,15.46383795
24.99323953,14.51325038,15.46406155
24.99570558,14.51405983,15.46426528
24.99795258,14.51479738,15.46445093
25,14.51546943,15.46462005
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
width=\w cm, height=\h cm,
scale only axis,
axis x line*=bottom,
axis y line*=left,
table/x index={0},thick,
xlabel=$x$,
ylabel=$y$,
]
\foreach \ind in {1,2}{
\addplot table[y index={\ind}, col sep=comma, mark=none] {mydata.csv};
}
\end{axis}
%%%%%%%%%%
\begin{axis}[
width=\w cm, height=\h cm,
scale only axis,thick,
axis x line*=top,
axis y line*=right,
xtick={3,7},
xticklabels={a,b},
yticklabels=none
]
\foreach \ind in {1,2}{
\addplot[opacity=0] table[y index={\ind}, col sep=comma, mark=none] {mydata.csv};
}
\end{axis}
\end{tikzpicture}
\end{document}


• Can it be define a function that extracts table.min and table.max values? In case the data is "big", this can reduce plotting cost of the data secondly. – mert Jul 14 '18 at 11:37
• Yes, this is enough for me. But there is a post here, which explain a similar concept that I can't adjust it to this problem. This could be a complete solution. – mert Jul 14 '18 at 11:55
• @mert Yes, this is indeed very useful if you need the extrema of some columns (even though I am not convinced that this is the simplest solution, I do not know the details but it seems that there advanced methods.) However, if the data is really big, and if you just want to draw some annotations, the above strategy may be advantageous. – user121799 Jul 14 '18 at 12:01