具有两个 y 轴的 PGF 条形图

具有两个 y 轴的 PGF 条形图

我在条形图中添加了一个新轴,其比例与第一个轴不同。
第二个轴发生了偏移,并且高度与第一个轴不同。
我需要进行哪些更改才能使右侧 Y 轴不发生偏移,并且线条从左轴延伸到右轴?到目前为止,我的代码如下:

\documentclass[12pt,tikz, border=10pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = \textwidth,
        height = 9cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Time},
        xlabel = {Subject},
        symbolic x coords={1,2,3,4,5},
        xtick = data,
            scaled y ticks = false,
            enlarge x limits=0.25,
            ymin=0,
        legend columns=2,
        legend cell align=left,
        legend style={
                at={(0.5,-0.2)},
                anchor=north,
                column sep=1ex
        }
    ]
        \addplot[style={blue,fill=blue,mark=none}]
           coordinates {(1, 09.06) (2, 09.52) (3, 12.20) (4, 12.10) (5, 14.05)};

        \addplot[style={red,fill=red,mark=none}]
             coordinates {(1, 19.22) (2, 21.29) (3, 21.58) (4, 23.27) (5, 37.03)};

        \legend{Data1, Data2}
    \end{axis}

    \begin{axis}[
    scale only axis,
    axis y line*=right,
    axis x line=none,
    width = \textwidth,
    height = 9cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    enlarge x limits=0.25,
    bar width=14pt,
    ylabel = {Advantage},
    xmin=0, xmax=6,
    ymin=0, ymax=4,
    legend columns=2,
    legend cell align=left,
    legend style={
        at={(0.5,-0.2)},
        anchor=north,
        column sep=1ex
    }
    ]
    \addplot[style={green,fill=green,mark=none}]
    coordinates {(1, 2) (2, 2) (3, 2) (4, 2) (5,2)};

    \addplot[green,sharp plot,update limits=false] coordinates {(0,1.5) (6,1.5)} node[above] at (axis cs:3,1.5) {1.5};


    \legend{advantage}
    \end{axis}

\end{tikzpicture}
\end{document}

答案1

您必须对两个轴上的 y 刻度使用相同的属性,例如

        scaled y ticks = false,
        ymin=-0.1,ymax=40,

并删除scale only axis,。此外,您必须在同一axis环境中绘制所有条形图,以便它们间距适当。我已经注释掉了不必要的行,我们保留第二个axis环境,仅用于右侧 y 轴。此外,我绘制了第二个 x 轴并使用,axis on top以便条形图不会覆盖 x 轴。更改太多,我无法全部写出来。请查看代码。

\documentclass[12pt, border=10pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = \textwidth,
        axis y line*=left,
        axis x line=bottom,
        height = 9cm,
        major x tick style = transparent,
        %axis on top,
        ybar=5*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Time},
        xlabel = {Subject},
        symbolic x coords={1,2,3,4,5},
        xtick = data,
            scaled y ticks = false,
            enlarge x limits=0.25,
            axis line style={-},
            ymin=-0.1,ymax=40,
        legend columns=2,
        legend cell align=left,
        legend style={
                at={(0.5,-0.2)},
                anchor=north,
                column sep=1ex
        }
    ]
        \addplot[style={blue,fill=blue,mark=none}]
           coordinates {(1, 09.06) (2, 09.52) (3, 12.20) (4, 12.10) (5, 14.05)};

        \addplot[style={red,fill=red,mark=none}]
             coordinates {(1, 19.22) (2, 21.29) (3, 21.58) (4, 23.27) (5, 37.03)};
        \addplot[style={green,fill=green,mark=none}]
             coordinates {(1, 2) (2, 2) (3, 2) (4, 2) (5,2)};
        \legend{Data1, Data2,Advantage}
    \end{axis}
    \begin{axis}[
    %scale only axis,
    axis y line*=right,
    axis x line=none,%axis on top,
    %xtick=\empty,
    width = \textwidth,
    height = 9cm,
    %major x tick style = transparent,
    %ybar=5*\pgflinewidth,
    enlarge x limits=0.25,
    axis line style={-},
    %bar width=14pt,
    ylabel = {Advantage},
    xmin=0, xmax=6,
    scaled y ticks = false,
    ymin=-0.1, ymax=40,
    %legend columns=2,
%    legend cell align=left,
%    legend style={
%        at={(0.5,-0.2)},
%        anchor=north,
%        column sep=1ex
%    }
    ]
    \addplot[green,sharp plot,update limits=false] coordinates {(0,1.5) (6,1.5)} node[above,pos=0.5] {1.5};
    %\legend{advantage}
    \end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容