pgfplots-第二个 y 轴没有数据

pgfplots-第二个 y 轴没有数据

我想在我的图中创建第二个 y 轴,但不包含数据。这样,在我的特殊情况下,左侧 y 轴是波数,右侧是波长。我已经设法获得了第二个轴,但现在这也应该显示正确的值:波长 = 1/波数。我该如何实现?

\documentclass[10pt, a4paper, twoside,ngerman]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[h]
    \begin{tikzpicture}
    \pgfplotstableread{1.txt} \datA

    \begin{axis}[
    axis y line*=left,
    ylabel near ticks,
    xlabel near ticks,
    xlabel=Temperatur in K,
    ylabel=Wellenzahl in cm-1,
    every x tick scale label/.style={at={(xticklabel cs:1)},anchor=south west},
    every axis plot/.append style={mark=none} , 
    legend pos=north west,
    legend style={draw=none, fill=none},
    title style={yshift=0.75cm},
    title=(a),
    ]
    \addplot table [x index = 0, y index = 1] from \datA ; 
    \end{axis}

    \begin{axis}[
    axis y line*=right,
    axis x line=none,
    ylabel near ticks,
    xlabel near ticks,
    y dir=reverse,
    ylabel=Wellenlänge in nm,
    every x tick scale label/.style={tick style={draw=none}},
    every axis plot/.append style={mark=none}
    ]
    \end{axis}

    \end{tikzpicture}
\end{figure}
\end{document}

1.txt:

1    3000
2    3001
3    3002
4    3003
5    3004

答案1

正如 Marmot 在在问题下方评论一种可能性是使用y coord trafo/y coord inv trafo选项来实现您想要的效果。有关详细信息,请查看代码中的注释。

% used PGFPlots v1.16
    \begin{filecontents*}{1.txt}
        1    3000
        2    3001
        3    3002
        4    3003
        5    3004
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
    \pgfplotsset{
        compat=1.3,
        /pgf/declare function={
            ymin=3000;
            ymax=3010;
        },
    }
    \pgfplotstableread{1.txt}{\datA}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ymin=ymin,
        ymax=ymax,
        %
        axis y line*=left,
        xlabel=Temperatur in K,
        ylabel=Wellenzahl in cm$^{-1}$,
    ]
        \addplot+ [thick] table {\datA};
    \end{axis}
    \begin{axis}[
        % if you have added axis limits to the first `axis' environment, you
        % also need to add them here. But because the coordinate transformation
        % is an inverse function, you need to swap the axis limit values
        % (ymin <--> ymax)
        ymin=ymax,
        ymax=ymin,
        %
        axis x line=none,
        axis y line*=right,
        y dir=reverse,
        ylabel=Wellenlänge in µm,
        %
        % use coordinate transformation to do what you want ...
        y coord trafo/.code=\pgfmathparse{10000/#1},
        y coord inv trafo/.code=\pgfmathparse{#1},
        % ... Also adapt the number format of the ticks.
        yticklabel={\pgfmathprintnumber[precision=3,zerofill]{\tick}},
    ]
%        % you need to add an `\addplot' command so that the ylabels are applied
%        % thus we can add an invisible plot
%        \addplot [draw=none,forget plot] table {\datA};

        % this `\addplot is just to demonstrate that the coordinate transformation
        % is done correctly
        \addplot+ [red,mark=square] table {\datA};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

相关内容