1

I would like to add a second x axis to my single plot. The first x axis goes from 0 to 2, the second one goes from 0 to 0.28. 0.28 on the second axis should be on the same x coordinate as 2 on the first one.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.16}


\begin{document}
   \begin{tikzpicture}[scale=1.3,transform shape]
        \begin{axis}[
        xlabel={Spulenstrom [A]},
        ylabel={Spitze der Volumenverteilung [nm]},
        xmin=0,
        ymin=0,
        scale only axis,
        ]
        \addplot+[black, mark options ={fill = black}, mark=*, error bars/.cd, y dir=both, y explicit,] coordinates {
        (0.25, 53.92) +- (21.76, 21.79)
        (0.5, 33.9) +- (13.81,13.81)
        (0.75, 29.54) +- (11.72, 11.72)
        (1, 27.35) +- (12.42, 12.42)
        (1.25, 30.38) +- (12.66, 12.66)
        (1.5, 24.97) +- (15.08, 15.08)
        (1.75, 34.46) +- (16.92, 16.92)
        (2, 30.2) +- (13.34, 13.34)
        };
        \end{axis}
        \begin{axis}[
        xlabel = {magnetische Flussdichte [T]},
        xmin = 0, xmax = 0.28, axis x line*=top, axis y line = none, scale only axis,
        ]
        \end{axis}
    \end{tikzpicture}
\end{document}

This is my current code which isn't working. I tried several examples for second y and x axes but all of them assume a second plot.

https://i.stack.imgur.com/riLIB.png This is the picture I currently get with this code

1
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – hpekristiansen Nov 18 '20 at 14:53
1

I managed to fix it, but it seems like a really crufty hack. I just plotted the same data again but made it transparent.

    \begin{tikzpicture}[]
        \begin{axis}[
        xlabel={Spulenstrom [A]},
        ylabel={Spitze der Volumenverteilung [nm]},
        xmin=0, 
        ymin=0,
        scale only axis,
        ]
        \addplot+[black, mark options ={fill = black}, mark=*, error bars/.cd, y dir=both, y explicit,] coordinates {
        (0.25, 53.92) +- (21.76, 21.79)
        (0.5, 33.9) +- (13.81,13.81)
        (0.75, 29.54) +- (11.72, 11.72)
        (1, 27.35) +- (12.42, 12.42)
        (1.25, 30.38) +- (12.66, 12.66)
        (1.5, 24.97) +- (15.08, 15.08)
        (1.75, 34.46) +- (16.92, 16.92)
        (2, 30.2) +- (13.34, 13.34)
        };
        \end{axis}
        
        \begin{axis}[
        xlabel = {Magnetische Flussdichte [T]}, xmin = 0, ymin = 0, tick label style={/pgf/number format/fixed},
        axis x line*=top, axis y line = none, scale only axis, xtick = {0, 0.07, 0.14, 0.21, 0.28}
        ]
        \addplot+[black, draw opacity = 0, fill opacity = 0, mark options ={fill = black}, mark=*, error bars/.cd, y dir=both, y explicit, ] coordinates {
        (0.035, 53.92) +- (21.76, 21.79)
        (0.07, 33.9) +- (13.81,13.81)
        (0.105, 29.54) +- (11.72, 11.72)
        (0.14, 27.35) +- (12.42, 12.42)
        (0.175, 30.38) +- (12.66, 12.66)
        (0.21, 24.97) +- (15.08, 15.08)
        (0.245, 34.46) +- (16.92, 16.92)
        (0.28, 30.2) +- (13.34, 13.34)
        };
        \end{axis}
    \end{tikzpicture}
1
  • 1
    Maybe just this \addplot[draw opacity = 0, fill opacity = 0] coordinates {(0.0) (0.28, 0)};. I would think it better to have the data only one place. – hpekristiansen Nov 19 '20 at 8:32

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.