0

I have two y-axes for the same (text) x-axis. I want the second axis to have only datapoints for C, D, E. When I do that, they don´t align correctly: they appear above A, C and E (spread over the x axis).

\documentclass[
12pt,
a4paper,
]{scrbook}

    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{lmodern}
    \usepackage[dvipsnames,table,xcdraw]{xcolor}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.18}
    
    \begin{document}
        \begin{figure}
            \centering
            \begin{tikzpicture}
                \pgfplotsset{
                    show sum on top/.style={
                        /pgfplots/scatter/@post marker code/.append code={%
                            \node[
                            at={(normalized axis cs:%
                                \pgfkeysvalueof{/data point/x},%
                                \pgfkeysvalueof{/data point/y})%
                            },
                            anchor=south,
                            color=black,
                            font=\footnotesize,
                            ]
                            {\pgfmathprintnumber[precision=2]{\pgfkeysvalueof{/data point/y}}};
                        },
                    },
                }
                \begin{axis}[
                    width=0.7\textwidth,
                    ybar stacked,
                    bar width=20pt,
                    nodes near coords={},
                    ymin=0, ymax=16,
                    tick label style={font=\footnotesize},
                    xlabel style={font=\footnotesize},
                    ylabel style={font=\footnotesize},
                    ylabel={Total values},
                    symbolic x coords={A, B, C, D, E}, 
                    xtick=data
                    ]
                    \addplot+[fill=CornflowerBlue,  draw=black,ybar] plot coordinates {(A,3.34) (B,3.25) (C,1.95) (D,4.4468) (E,3.05)}; 
                    \addplot+[fill=orange,          draw=black,ybar] plot coordinates {(A,0.43) (B,0.25) (C,1.06) (D,0.7987) (E,0.74)};
                    \addplot+[fill=YellowGreen,     draw=black,ybar] plot coordinates {(A,0.97) (B,0.98) (C,2.11) (D,1.4551) (E,1.46)};
                    \addplot+[fill=Goldenrod,       draw=black,ybar,show sum on top] plot coordinates {(A,0.6) (B,0.61) (C,1.73) (D,1.1238) (E,1.12)};
                \end{axis}
                
                \begin{axis}[
                    width=0.7\textwidth,
                    axis x line=none,
                    axis y line=none,
                    ymin=0, ymax=16,
                    legend pos=north west,
                    ytick=\empty,
                    xtick=\empty,
                    symbolic x coords= {C, D, E},
                    xticklabels={C, D, E}
        %       xtick=data
                    ]
                    \addplot [Lavender,mark=diamond*, only marks, draw=black,mark size=6pt]  plot coordinates {(C,15) (D,12) (E,12)};
                \end{axis}
            \end{tikzpicture}
        \end{figure}
    \end{document}
4
  • idk why I cannot paste pictures today (error: "Failed to upload image; an error occurred on the server") Jan 30 at 15:15
  • The imgur server is down. Nevermind. Jan 30 at 15:22
  • 1
    You can add a phantom plot via \addplot[] plot coordinates {(A,0) (E,0)}; to the second axis environment which will ensure the right positioning. Jan 30 at 16:33
  • Ohhhh, that worked. Thanks so much, that was so fiddly. Jan 30 at 16:43

1 Answer 1

1

There is a lot of unrelated code in your MWE, so I will not edit and repost it. You can use a dummy plot \addplot[] plot coordinates {(A,0) (E,0)}; as suggested by @Jasper Habicht or better: \addplot[forget plot] plot coordinates {(A,0) (E,0)};

-You could also make the scales match with these options:

symbolic x coords={A, B, C, D, E}, xmin=A, xmax=E, enlarge x limits=true,

You must log in to answer this question.

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