5

I want to plot Wavelength and Wavenumbers of one Dataset to one tikzpicture. I found stackexchange: How to have linked axes on plots? but I DONT want to have both axis-ticks aligned to same position. See image: red is plotted but blue does not fit with buttom axes and is unfortunately not plotted.

enter image description here

Like Jakes code:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}
\pgfkeys{/pgfplots/.cd,master axis/.style={
    scale only axis,
    enlarge x limits=false,
    axis x line*=bottom,
    xticklabel shift=3pt,
    after end axis/.code={
      \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
      \pgfmathparse{\pgfkeysvalueof{/pgfplots/xmin}}
      \global\let\masterxmin=\pgfmathresult
      \pgfmathparse{\pgfkeysvalueof{/pgfplots/xmax}}
      \global\let\masterxmax=\pgfmathresult
      \pgfkeys{/pgf/fpu=false}
    }
  },
  slave axis/.style={
    scale only axis,enlarge x limits=false,
    axis x line*=top,
    axis y line=none,
    xmin=\masterxmin,xmax=\masterxmax,ymin=0,ymax=1,
    scaled x ticks=false,
    xtick={1,4,10,20,40,100},
    xticklabel={
      \pgfkeys{/pgf/fpu}
      \pgfmathparse{1/\tick}
      \pgfmathprintnumber{\pgfmathresult}
    }
  }
}
\begin{document}
\begin{tikzpicture}

\begin{axis}[master axis,
  ymin=0,ymax=100,
  xmin=7,xmax=16,
  enlarge x limits=false,
  xlabel={Wavelength $\lambda$},
  ylabel=Transmissivity
]
\addplot[domain=7:16,samples=100,thick]{-(abs(tan(x*10))-2*rnd)+90};
\end{axis}

\begin{axis}[slave axis,xlabel=Wavenumber 1/$\lambda$]\end{axis}

\end{tikzpicture}
\end{document}

so how to change the code? One posibility is to leave or change

xtick={1,4,10,20,40,100}

but this leads in badly brocken values align with bottom (see Image 2). bad numbers

I also tried to set to

xmin=0.1428,xmax=0.0625,x dir=reverse %   1/7=0.1428   1/16=0.0625

but this destroys the axis.

I also found stackexchange:Use function to scale x axis but I don't know how to change this because of the reverse counting.

Thanks a lot :-)

  • Could you explain what you mean by "don't want to have both axis ticks aligned to the same position"? – Leeser Apr 28 '14 at 19:56
  • I add another image that shows the plot with align axe-ticks (and the values are rounded -> It woud by great if the ticks would change position so that the values are correct). – Thomas Apr 28 '14 at 23:28
4

You can change the xtick values to

% 8.33=1/0.12, 9.09=1/0.11, 1=1/0.1, 11.11=1/0.09, ...
xtick={8.33,9.09,10,11.11,12.5,14.29},

Then you get

enter image description here

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}
\pgfkeys{/pgfplots/.cd,master axis/.style={
    scale only axis,
    enlarge x limits=false,
    axis x line*=bottom,
    xticklabel shift=3pt,
    after end axis/.code={
      \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
      \pgfmathparse{\pgfkeysvalueof{/pgfplots/xmin}}
      \global\let\masterxmin=\pgfmathresult
      \pgfmathparse{\pgfkeysvalueof{/pgfplots/xmax}}
      \global\let\masterxmax=\pgfmathresult
      \pgfkeys{/pgf/fpu=false}
    },
  },
  slave axis/.style={
    scale only axis,enlarge x limits=false,
    axis x line*=top,
    axis y line=none,
    xmin=\masterxmin,xmax=\masterxmax,
    ymin=0,ymax=1,
    scaled x ticks=false,
    xtick=\slavextick,
    xticklabel={%
      \pgfkeys{/pgf/fpu}%
      \pgfmathparse{1/\tick}%
      \pgfmathprintnumber{\pgfmathresult}%
    },
      xticklabel style={/pgf/number format/.cd,fixed,precision=2},
      xmajorgrids=true, % to show the tick position
      every axis x grid/.style={red,dashed,very thick},
  }
}
  % calculation of the xtick position
  \xdef\slavextick{}
  \foreach[count=\n][evaluate=\i as \j using 1/\i]%
    \i in {0.12,0.11,...,0.07} % xtick for the slave axis
    {\pgfmathparse{\n==1?"":","}\xdef\slavextick{\slavextick\pgfmathresult\j}
    }

\begin{document}
\begin{tikzpicture}

\begin{axis}[master axis,
  ymin=0,ymax=100,
  xmin=7,xmax=16,
  enlarge x limits=false,
  xlabel={Wavelength $\lambda$},
  ylabel=Transmissivity,
]
\addplot[domain=7:16,samples=100,thick]{-(abs(tan(x*10))-2*rnd)+90};
\end{axis}

\begin{axis}[slave axis,xlabel=Wavenumber 1/$\lambda$]\end{axis}

\end{tikzpicture}
\end{document}
  • oh... That's true :-) but is is possible to automatically calculate this values (like for normal axis)? properly not but it would by very nice. – Thomas Apr 29 '14 at 10:43
  • @Thomas I have changed the code a little bit. – esdd Apr 29 '14 at 12:09
  • cool ... Slowly I understand the way tikzpicture works. Thank you! – Thomas Apr 29 '14 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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