将次要 x 轴限制设置为与数据图上的第一个限制相同

将次要 x 轴限制设置为与数据图上的第一个限制相同

我想绘制一个有两个 x 轴(顶部和底部)和一个 y 轴的数据。第一个 x 轴和 y 轴是从数据中设置的。第二个 x 轴的限制应该与第一个相同。我还需要在第二个 x 轴上标记一些刻度。Pgfplots 不允许我标记(见图)。

我怎样才能取得成果?

在此处输入图片描述

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

答案1

更新:如果您确实只想添加 a 和 b 标签,则不需要所有双轴技巧。

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

在此处输入图片描述

相关内容