2

I want to make a 2nd y-axis in my plot but without data. So that at the left y-axis is the wavenumber and on the right the wavelength in my special case. I've already managed to get the 2nd axis but now this should also show the correct values: wavelength=1/wavenumber. How can I achieve this?

\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
5
  • What do you want to see in that sample. But okay, I added it. Commented Sep 4, 2018 at 20:41
  • @marmot well okay, I changed it now to a minimal working example but that's not the point. In my document it was compiling and everything, so no one needs to search errors in my code! I just want to know how I can make the second y-axis accordingly. Commented Sep 4, 2018 at 21:01
  • I didn't add the data cause it's just not free to use for everyone. The data is protected but I added some random data if it helps for explaining. The problem is not, that I don't geht the ylabel. The think is, that left y axis goes, correctly from 3000 to 3004. So my right y axis needs to run from 3.333e-9 to 3.329e-9. Commented Sep 4, 2018 at 21:18
  • You may understand that I am no longer in answering mood. Try something like yticklabel={\pgfmathprintnumber[sci,precision=6]{\tick}}, y coord trafo/.code=\pgfmathparse{3000/#1}, y coord inv trafo/.code=\pgfmathparse{#1/3000}, . And use filecontents to make life simpler for others, i.e. add \begin{filecontents}{1.txt} 1 3000 2 3001 3 3002 4 3003 5 3004 \end{filecontents} to the preamble of your document. Commented Sep 4, 2018 at 21:51
  • Did marmot's comment or my answer help you to solve your problem or do you need further assistance? Commented Sep 15, 2018 at 5:41

1 Answer 1

1

As Marmot already suggested in the comment below the question one possibility is to use the y coord trafo/y coord inv trafo options to achieve what you want. For details please have a look at the comments in the code.

% 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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.