1

I'm plotting an ybar (with error bars) and a standard mark (with error bars) diagram with two different ordinates using two axis environments nested inside a tikzpicture environment.

The problem is now, that the marks are centered between the ybars and not centered to the respective ybar.

How can I shift the marks with their error bars to the respective ybar (blue marks centered to blue bars, red marks centered to red bars)?

Sadly, mark options={xshift=\pgfkeysvalueof{/pgf/bar shift}} does not work as I'm using two axis-environments.

I also have a second little problem: How can I align the ygrids to let the plot look more clean?

Thanks a lot for your help! Any input is appreciated!

This is how my plot looks right now:enter image description here

This is my code right now:

\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

The formula for the effective bar shift can be found at the top of p. 83 of the pgfplots manual 1.16. you only need to shift the marks by minus or plus 6pt. However, the legend is bit more complicated.

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.