将两个纵坐标图中的标记与相应的 ybar 对齐

将两个纵坐标图中的标记与相应的 ybar 对齐

我正在使用嵌套在一个环境axis中的两个环境绘制具有两个不同坐标的 ybar(带有误差线)和标准标记(带有误差线)图。tikzpicture

现在的问题是,标记位于 ybar 之间的中心,而不是位于相应 ybar 的中心。

我怎样才能将标记及其误差线移动到相应的 ybar(蓝色标记以蓝色条为中心,红色标记以红色条为中心)?

遗憾的是,由于我使用了两个轴环境,因此标记options={xshift=\pgfkeysvalueof{/pgf/bar shift}}不起作用。

我还有第二个小问题:如何对齐 ygrids 以使图看起来更清晰?

非常感谢您的帮助!任何意见都值得赞赏!

这就是我的情节现在的样子:在此处输入图片描述

这是我现在的代码:

\begin{tikzpicture}
    \pgfplotsset{set layers}
\begin{axis}[
    symbolic x  coords={430,440,445,450},
    xtick={430,440,445,450},
    xticklabels={430,440,445,450},
    axis y line*=left,
    ymajorgrids={true},
    grid style={dashed,gray!30}, 
    ybar, 
    xtick align = inside,
    legend style={at={(-0.25,0.5)},anchor=east},
    ylabel={Tensile Strength in \si{MPa}},
    xlabel={Nozzle Temperature in \si{\degreeCelsius}},
    ]
    \addplot+[
error bars/.cd,
y dir=both,
y explicit,
]
coordinates{
    (430,18.1) +- (0,1.73)%PC6
    (440,18.5) +- (0,0.692)%PC4
    (445,16.1) +- (0,3.09)%PC8
    (450,18.3) +- (0,1.8)%PC9
};  
\addlegendentry{50ProzentCooling}   
    \addplot+[
error bars/.cd,
y dir=both,
y explicit,
]
coordinates{
    (430,23.1) +- (0,2.15)%PC5
    (440,27.5) +- (0,1.17)%PC3
    (445,26.1) +- (0,3.14)%PC7
    (450,22.7) +- (0,4.53)%PC1
};
\addlegendentry{100ProzentCooling}      

    \end{axis}
    \begin{axis}[
    axis x line=none,
    xtick=\empty,
    symbolic x  coords={430,440,445,450},
    ymajorgrids={true},
    grid style={dashed,gray!30},        
    yticklabel pos=right,
    ylabel near ticks,
    ylabel={Deviation in \si{mm}},
    legend style={at={(1.25,0.5)},anchor=west},
    ]
        \addplot+[
        %mark options={xshift=\pgfkeysvalueof{/pgf/bar shift}},
        error bars/.cd,
        y dir=both,
        y explicit,
        ] coordinates {
        (430,0.24015)  +- (0,0.00586899)%PC6
        (440,0.28105) +- (0,0.00685894)%PC4
        (445,0.3283) +- (0,0.00254558) %PC8
        (450,0.37125) +- (0,0.00360624)%PC9
    };  \addlegendentry{50ProzentCooling}   


    \addplot+[
    error bars/.cd,
    y dir=both,
    y explicit,
    ] coordinates {
    (430,0.14455)+- (0,0.00615183) %PC5
    (440,0.15095)+- (0,0.00417193) %PC3
    (445,0.17985)+- (0,0.00558614) %PC7
    (450,0.2516)+- (0,0.04412346) %PC1  
};  \addlegendentry{100ProzentCooling}  

        \end{axis}
\end{tikzpicture}

答案1

有效条形位移的公式可以在 pgfplots 手册 1.16 第 83 页的顶部找到。您只需将标记移动负或正 6pt。但是,图例有点复杂。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{% https://tex.stackexchange.com/a/355720/121799
  /pgfplots/error bar legend/.style={
    legend image code/.code={
        \draw[sharp plot,mark=-,mark repeat=2,mark phase=1,##1,my legend]
        plot coordinates { (0.3cm, -0.15cm) (0.3cm,0cm) (0.3cm, 0.15cm) };%
        \draw[mark repeat=2,mark phase=2,only marks,##1,my legend]
        plot coordinates {(0cm,0cm) (0.3cm,0cm) (0.6cm,0cm)};%
        }}}
\begin{document}
\begin{tikzpicture}[my legend/.style={}]
    \pgfplotsset{set layers}
\begin{axis}[
    symbolic x  coords={430,440,445,450},
    xtick={430,440,445,450},
    xticklabels={430,440,445,450},
    axis y line*=left,
    ymajorgrids={true},
    grid style={dashed,gray!30}, 
    ybar, 
    xtick align = inside,
    legend style={at={(-0.25,0.5)},anchor=east},
    ylabel={Tensile Strength in \si{MPa}},
    xlabel={Nozzle Temperature in \si{\degreeCelsius}},
    ]
    \addplot+[
      error bars/.cd,
      y dir=both,
      y explicit,
      ]
      coordinates{
          (430,18.1) +- (0,1.73)%PC6
          (440,18.5) +- (0,0.692)%PC4
          (445,16.1) +- (0,3.09)%PC8
          (450,18.3) +- (0,1.8)%PC9
      };  
    \addlegendentry{50ProzentCooling}   
          \addplot+[
      error bars/.cd,
      y dir=both,
      y explicit,
      ]
      coordinates{
          (430,23.1) +- (0,2.15)%PC5
          (440,27.5) +- (0,1.17)%PC3
          (445,26.1) +- (0,3.14)%PC7
          (450,22.7) +- (0,4.53)%PC1
      };
      \addlegendentry{100ProzentCooling}      
\end{axis}
\begin{axis}[error bar legend,
    axis x line=none,
    xtick=\empty,
    symbolic x  coords={430,440,445,450},
    ymajorgrids={true},
    grid style={dashed,gray!30},        
    yticklabel pos=right,
    ylabel near ticks,
    ylabel={Deviation in \si{mm}},
    legend style={at={(1.25,0.5)},anchor=west},
    ]
    \addplot+[%transform canvas={xshift=-6pt},
        xshift=-6pt,forget plot,
        %mark options={xshift=-6pt},
        error bars/.cd,
        y dir=both,
        y explicit,
        ] coordinates {
        (430,0.24015)  +- (0,0.00586899)%PC6
        (440,0.28105) +- (0,0.00685894)%PC4
        (445,0.3283) +- (0,0.00254558) %PC8
        (450,0.37125) +- (0,0.00360624)%PC9
    }; 
    \addlegendimage{mark=*,color=blue}
    \addlegendentry{50ProzentCooling}   


    \addplot+[mark options={color=red},
    xshift=6pt,forget plot,color=red,
        %mark options={xshift=6pt},
    error bars/.cd,
    y dir=both,
    y explicit,
    ] coordinates {
    (430,0.14455)+- (0,0.00615183) %PC5
    (440,0.15095)+- (0,0.00417193) %PC3
    (445,0.17985)+- (0,0.00558614) %PC7
    (450,0.2516)+- (0,0.04412346) %PC1  
    };  
    \addlegendimage{mark=*,color=red,}
    \addlegendentry{100ProzentCooling}  


        \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容