具有两个 y 轴但相同的 x 轴的 Pgf 图:如何才能在第二个轴上仅显示特定 x 值的 y 值?

具有两个 y 轴但相同的 x 轴的 Pgf 图:如何才能在第二个轴上仅显示特定 x 值的 y 值?

我有两个 y 轴,对应同一个(文本)x 轴。我希望第二个轴只包含 C、D、E 的数据点。当我这样做时,它们无法正确对齐:它们出现在 A、C 和 E 上方(分布在 x 轴上)。

\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}

答案1

您的 MWE 中有很多不相关的代码,因此我不会编辑并重新发布它。您可以\addplot[] plot coordinates {(A,0) (E,0)};按照@Jasper Habicht 的建议使用虚拟图,或者更好的方法: \addplot[forget plot] plot coordinates {(A,0) (E,0)};

-您还可以使尺度与以下选项相匹配:

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

相关内容