1

I want to produce the following plot:

lorentzian

Is a Lorentzian function, centered at x=408. But notice the upper label in the frame, that indicates a wave number associated to the coordinate measured in pixels (the bottom label).

I came across a few questions regarding plotting two x axes in pgfplots. (For example here and here), but they all are concerned about plotting two functions or curves on the same plot, and labeling them differently. I have only one function that I want to label twice.

What I have at the moment is this:

\documentclass{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
xlabel={CCD coordinate [px]},
ylabel={Intensity [A.U]},
xmin=0,xmax=1000,
ymajorgrids=true,
xmajorgrids=true,
]
\addplot[line width=1pt,samples=500,domain=0:1000]{4.278*10^(7)/((x-408.4)^2+54.25^2)};
\end{axis}
\end{tikzpicture}
\end{document}

I want the upper label to consist the following markers, written in the form

{{x1,label1},{x2,label2}, ... }

{80.15,639.892},{244.225,648.096},{408.3,656.3},{572.375,664.504},{736.45,672.708},{900.525,680.911}

What option should I add to my code to make it?

tush
  • 1,115

1 Answers1

4

I also needed this plot in the past, so here is my result:

enter image description here

\documentclass[border=5pt]{standalone}

\usepackage{pgfplots}

\pgfplotsset{ compat=1.17, % create a style for the plot, so you don't need to repeat yourself % in both axis environments. Lorentzian plot style/.style={ every tick label/.append style={font=\small}, scaled y ticks=false, xmin=0, xmax=1000, % keep domain limits in sync with the x-axis limits domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}, }, }

\begin{document} \begin{tikzpicture} \begin{axis} [ % load the above created style Lorentzian plot style, xlabel={CCD coordinate [px]}, ylabel={Intensity [a.u.]}, axis x line*=left, ymajorgrids=true, xmajorgrids=true, ] \addplot[ line width=1pt, samples=501, ] {4.278e7/((x - 408.4)^2 + 54.25^2)}; \end{axis}

    % produce top x-axis
    \begin{axis}[
        % load the above created style here too
        Lorentzian plot style,
        xlabel={Wavelength [nm]},
        axis x line*=right,
        xtick={80.15,244.225,408.3,572.375,736.45,900.525},
        xticklabels={639.9,648.1,656.3,664.5,672.7,680.9},
        ytick=\empty,
        ]
        \addplot[
        draw=none,
        % for dummy use minimum number of `samples`
        samples=2 
        % dummy function to make it as less computational expensive as possible
        ]{0};
    \end{axis}
\end{tikzpicture}

\end{document}

First, I removed the right x axis of the main plot using axis x line*=left and then I added the same plot, but did not draw it with draw=none, added the right x axis using axis x line*=right and your labels with xtick={...} and xticklabels={...}, and at the end removed the y axis and labels of the empty plot using ytick=\empty.

Then, I recognised some overlap of the y label scaling factor, so I removed it using scaled y ticks=false and also decreased the size of the x axis labels using every tick label/.append style={font=\small}.

Hopefully, it suits your needs.

EDIT

I added @Stefan Pinnows excellent suggestions for improving the workflow and placed some of his comments for better understanding the changes.

Stefan Pinnow
  • 29,535
Excelsior
  • 2,322
  • For the y label the unit should be a.u. (arbitrary unit), or am I wrong? – Excelsior Apr 13 '21 at 22:51
  • +1 You were absolutely right to add a second axis and state corresponding xticks and corresponding xticklabels. But there is room for a bit of improvement to your answer. Since this would be a bit too long for a comment, I posted it as separate answer. Please have a look if you find some useful stuff there. If you would adapt these stuff in your answer, I'll be happy to delete mine. – Stefan Pinnow Apr 14 '21 at 05:08
  • @StefanPinnow Thanks for your improvements. My answer was me trying to solve the problem years ago when I started with pgfplots. I will integrate your suggestions for less code and faster processing. – Excelsior Apr 14 '21 at 06:57
  • I improved the comments a bit ... – Stefan Pinnow Apr 14 '21 at 08:35