3

For my scatter plot I have two y-axis, but they are proportional, meaning the left values are divided by a constant. Therefore, all the circles and stars are at the same position. When enabling the error bars for the circle plot (i.e. error bars/.cd, y dir=both, y explicit), the left axis changes to accomodate the error bars. However the stars are shifted to a different position (right image).

Is there a way to fix the axis so that both markers stay at the same position?

enter image description here enter image description here

MWE:

\documentclass{scrbook}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}\pgfkeys{/pgf/number format/read comma as period}\pgfplotsset{compat=1.16}

\usepackage{xcolor}
    \definecolor{mlila}{RGB}{136, 55, 155}
    \definecolor{mgrau3}{RGB}{153, 153, 153}

\begin{document}

\pgfplotstableread{
    Quadrant    Bereich d_m     d_s     r_d
    0           0       0,952   0,0016  94,2572
    0           0       0,9552  0,0009  94,5714
    I           a       0,9611  0,0006  95,155   
    I           a       0,9597  0,0008  95,0177
}\datad


\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={0,I},
    scatter/classes={0={mlila}, a={mgrau3}},
    height=\textwidth,
    width=0.21\textwidth,
    enlarge x limits=0.4,
    xtick=data,
    y tick label style={
        /pgf/number format/.cd,
        fixed zerofill,
        precision=3,
    /tikz/.cd
    },
    axis y line*=left,
    scale only axis
]

    \addplot[
        scatter,
        only marks,
        % error bars/.cd,
        % y dir=both,
        % y explicit
    ] table[meta=Bereich, scatter src=explicit symbolic, x=Quadrant, y=d_m, y error=d_s] {\datad};

\end{axis}

\begin{axis}[
    symbolic x coords={0,I},
    height=\textwidth,
    width=0.21\textwidth,
    enlarge x limits=0.4,
    axis y line*=right,
    xtick=data,
    scatter/classes={0={mlila}, a={mgrau3}},
    scale only axis
]

    \addplot[
        scatter,
        only marks,
        mark=star,
        mark options={scale=4}
    ] table[meta=Bereich, scatter src=explicit symbolic, x=Quadrant, y=r_d] {\datad};
\end{axis}
\end{tikzpicture}

\end{document}
1
  • You could of course set the y limits manually with ymin/ymax, but I guess you're looking for something more automatic ... Commented Jul 27, 2020 at 14:19

1 Answer 1

1

This is a manual workaround by setting appropriate ymin/ymax, hence not automatic. (I add this answer out of interest, not for earning reputation bounty.)

  • Firstly, choose ymin and ymax for first plot with y=d_m, y error=d_s. For example, use ymin=0.9491, ymax=0.9629.
  • Then, compute corresponding ymin and ymax for second plot with y=r_d.
  • By computation, we get ymin=93.9730, ymax=95.3308.
y1       delta1   y1 error y2       delta2   delta ratio
d_m               d_s      r_d      
0.952    0.0016   94.2572      
0.9552   0.0032   0.0009   94.5714  0.3142   0.010185
0.9611   0.0059   0.0006   95.155   0.5836   0.010110
0.9597  -0.0014   0.0008   95.0177 -0.1373   0.010197
                                             0.010164 delta ratio

y2 avg   0.9570   y2 avg   94.7503
y1 min   0.9491   y2 min   93.9730
y1 max   0.9629   y2 max   95.3308

y2 min = y2 avg + (y1 min - y1 avg) / ratio
y2 max = y2 avg + (y1 max - y1 avg) / ratio
\documentclass{scrbook}

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
  \pgfkeys{/pgf/number format/read comma as period}\pgfplotsset{compat=1.16}

\usepackage{xcolor}
    \definecolor{mlila}{RGB}{136, 55, 155}
    \definecolor{mgrau3}{RGB}{153, 153, 153}

\begin{document}

\pgfplotstableread{
    Quadrant    Bereich d_m     d_s     r_d
    0           0       0,952   0,0016  94,2572
    0           0       0,9552  0,0009  94,5714
    I           a       0,9611  0,0006  95,155   
    I           a       0,9597  0,0008  95,0177
}\datad


\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={0,I},
    scatter/classes={0={mlila}, a={mgrau3}},
    height=\textwidth,
    width=0.21\textwidth,
    enlarge x limits=0.4,
    xtick=data,
    y tick label style={
      /pgf/number format/.cd,
      fixed zerofill,
      precision=3,
      /tikz/.cd
    },
    axis y line*=left,
    scale only axis,
    ymin=0.9491, ymax=0.9629
]

    \addplot[
        scatter,
        only marks,
         error bars/.cd,
         y dir=both,
         y explicit
    ] table[meta=Bereich, scatter src=explicit symbolic, x=Quadrant, y=d_m, y error=d_s] {\datad};

\end{axis}

\begin{axis}[
    symbolic x coords={0,I},
    height=\textwidth,
    width=0.21\textwidth,
    enlarge x limits=0.4,
    axis y line*=right,
    xtick=data,
    scatter/classes={0={mlila}, a={mgrau3}},
    scale only axis,
    ymin=93.9730, ymax=95.3308
]

    \addplot[
        scatter,
        only marks,
        mark=star,
        mark options={scale=4}
    ] table[meta=Bereich, scatter src=explicit symbolic, x=Quadrant, y=r_d] {\datad};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .