pgfplots ybar 图具有 2 个 y 轴和 x 轴上的相等偏移

pgfplots ybar 图具有 2 个 y 轴和 x 轴上的相等偏移

我正在尝试创建一个具有两个 y 轴和 x 轴正确对齐的 ybar 图。我有两个数据集需要比较,我想要得到的是一个正常的 ybar 图,其中每个刻度都有水平移动的条形,但只有第二个 y 轴。我得到的是一个堆叠的 ybar 图。这是屏幕截图。

我得到了什么

我尝试过水平移动第二个条形图,但我只能将它们与 x 轴一起移动。当使第二个 x 轴不可见时,我可以使其看起来几乎正确,但只是几乎正确。轮廓框突然显示一个间隙(只有不可见的 x 轴位于其中),并且每个刻度周围的偏移不均匀。我希望两个条形图都均匀地围绕刻度移动,而不是第一个条形图直接位于刻度上,第二个条形图位于其右侧。

这是我的 MWE:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12,height=0.3\textheight,legend cell align=left,tick scale binop=\times}
\pgfplotsset{grid style={loosely dotted,color=darkgray!30!gray,line width=0.6pt},tick style={black,thin}}
\pgfplotsset{every axis plot/.append style={line width=0.8pt}}
\usepackage{pgfplotstable}

    \pgfplotstableread{
        Proben-Nr   Si-Proben   Si-Differenz-abs    Si-Differenz-rel
        1   V6  11.3593 28.1270
        2   V72 13.9331 37.8023
        3   V71 9.8126  30.0322
        4   V91 7.8221  39.3675
        5   V52 2.9802  17.0464
        6   V51 1.8607  11.0570
        7   V54 0.4743  3.0959
        8   V55 0.4464  3.1361
        9   V53 -0.3831 -2.7133
        10  V4  -6.1150 -46.745
        11  V31 0.5284  5.0706
        12  V3  -0.5891 -7.7555
        13  V32 -0.3487 -4.6406
        14  V2  -2.0608 -53.916
        15  V34 -4.2070 -153.28
        16  V35 -5.8963 -218.38
        17  V1  -4.3101 -260.54 
    }\Palermo

\begin{document}


    \begin{figure}[htbp]
        % Si Error
        \centering
        \begin{tikzpicture}
        \pgfplotsset{
            scale only axis,
            xmin=0,
            xmax=18,
            width=0.9\textwidth,
            height=0.30\textheight,
        }
        \begin{axis}[
        ymin=-15,
        ymax=15,
        axis y line*=left,
        ybar,
        bar width=0.25,
        xlabel={Probe},
        ylabel={Absoluter Fehler in at\%},
        xtick={1,2,...,17},
        xticklabel style={rotate=45,anchor=north east},
        xticklabels from table={\Palermo}{Si-Proben},
        xtick align=inside,
        xticklabel pos=left,
        grid,
        legend pos= south west
        ]
        \addplot [fill=blue,]
        table[x = Proben-Nr, y = Si-Differenz-abs,]{\Palermo};
        \legend{Absoluter Fehler}
        \end{axis}
        \begin{axis} [
        ymin=-300,
        ymax=300,
        axis y line*=right,
        axis x line*=none,
        ybar,
        bar width=0.25,
        ylabel={Relativer Fehler in \%},
        xtick={1,2,...,18},
        xticklabel style={rotate=45,anchor=north east},
        xticklabels from table={\Palermo}{Si-Proben},
        xtick align=inside,
        xticklabel pos=left,
        legend pos= north east
        ]
        \addplot [fill=red]
        table[x = Proben-Nr, y = Si-Differenz-rel]{\Palermo};
        \legend{Relativer Fehler}
        \end{axis}
        \end{tikzpicture}
        \caption{Si absolute und relative Abweichung}
    \end{figure}
\end{document}

答案1

用 移动红色条xshift=0.5*\pgfplotbarwidth,用 移动蓝色条xshift=-0.5*\pgfplotbarwidth

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12,height=0.3\textheight,legend cell align=left,tick scale binop=\times}
\pgfplotsset{grid style={loosely dotted,color=darkgray!30!gray,line width=0.6pt},tick style={black,thin}}
\pgfplotsset{every axis plot/.append style={line width=0.8pt}}
\usepackage{pgfplotstable}

    \pgfplotstableread{
        Proben-Nr   Si-Proben   Si-Differenz-abs    Si-Differenz-rel
        1   V6  11.3593 28.1270
        2   V72 13.9331 37.8023
        3   V71 9.8126  30.0322
        4   V91 7.8221  39.3675
        5   V52 2.9802  17.0464
        6   V51 1.8607  11.0570
        7   V54 0.4743  3.0959
        8   V55 0.4464  3.1361
        9   V53 -0.3831 -2.7133
        10  V4  -6.1150 -46.745
        11  V31 0.5284  5.0706
        12  V3  -0.5891 -7.7555
        13  V32 -0.3487 -4.6406
        14  V2  -2.0608 -53.916
        15  V34 -4.2070 -153.28
        16  V35 -5.8963 -218.38
        17  V1  -4.3101 -260.54 
    }\Palermo


\begin{document}


    \begin{figure}[htbp]
        % Si Error
        \centering
        \begin{tikzpicture}
        \pgfplotsset{
            scale only axis,
            xmin=0,
            xmax=18,
            width=0.9\textwidth,
            height=0.30\textheight,
        }
        \begin{axis}[
        ymin=-15,
        ymax=15,
        axis y line*=left,
        ybar,
        bar width=0.25,
        xlabel={Probe},
        ylabel={Absoluter Fehler in at\%},
        xtick={1,3,...,39},
        xticklabel style={rotate=45,anchor=north east},
        xticklabels from table={\Palermo}{Si-Proben},
        xtick align=inside,
        xticklabel pos=left,
        grid,
        legend pos= south west
        ]
        \addplot [fill=blue,xshift=-0.5*\pgfplotbarwidth]
        table[x = Proben-Nr, y = Si-Differenz-abs,]{\Palermo};
        \legend{Absoluter Fehler}
        \end{axis}
        \begin{axis} [
        ymin=-300,
        ymax=300,
        axis y line*=right,
        axis x line*=none,
        ybar,
        bar width=0.25,
        ylabel={Relativer Fehler in \%},
        xtick={2,4,...,38},
        xticklabel style={rotate=45,anchor=north east},
        xticklabels from table={\Palermo}{Si-Proben},
        xtick align=inside,
        xticklabel pos=left,
        legend pos= north east
        ]
        \addplot [fill=red,xshift=0.5*\pgfplotbarwidth]
        table[x = Proben-Nr, y = Si-Differenz-rel]{\Palermo};
        \legend{Relativer Fehler}
        \end{axis}
        \end{tikzpicture}
        \caption{Si absolute und relative Abweichung}
    \end{figure}
\end{document} 

输出

在此处输入图片描述

相关内容