1

I am working on a horizontal bar plot with error bars. In addition I'd like to place the actual value of each bar with nodes near coords in the plot. However they tend to overlap with the positive error bar. In some cases this is easy to fix with nodes near coords style={xshift=32pt}. However that only makes sense if the error bars are roughly within the same range. Is there any way to tie the node to the positive error bar's end?

\documentclass[a4paper, BCOR=4mm, fontsize=12pt, twoside]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{/pgf/number format/use comma}

\begin{document}

\begin{figure}[htb]
\centering
\begin{tikzpicture} 
\begin{axis}[xbar, bar width=0.7cm, width=0.7\textwidth,
                xmin=0, xmax=7, enlarge y limits = {abs = .8},
                xmajorgrids=true, xminorgrids=true,
                major y tick style={draw=none}, minor x tick num=1,
                minor grid style={dashed},
                ytick={-1,-2}, 
                yticklabels={A,B},
                yticklabel style={align=right},
                nodes near coords, nodes near coords style={xshift=32pt},
                legend style={cells={anchor=west},at={(0.5,-0.15)},anchor=north}, legend reversed,
                ]
                
\addplot [fill=red!30, error bars/.cd, x dir=both, x explicit] coordinates {
        (4.5,-1) +- (1.197,0)
        (3.88,-2) +- (0.995,0)
    };
    
\addplot [fill=green!30, error bars/.cd, x dir=both, x explicit] coordinates {
        (5.4,-1) +- (0.7,0)
        (4.2,-2) +- (1.508,0)
    };
    
\addplot [fill=yellow!30, error bars/.cd, x dir=both, x explicit] coordinates {
        (5.4,-1) +- (0.57,0)
        (4.21,-2) +- (0.833,0)
    };
    
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

3
  • 1
    Of course this is possible ;) Will you be able to adapt the solution given in tex.stackexchange.com/a/485859/95441 yourself to your needs? If you should fail, ping me and I'll write an answer here. Mar 19 at 4:45
  • Thanks for your hint! I get the concept but I don't know how to access the error bar value without table input. visualization depends on={<x error> \as \offset}, node near coords style={shift={(axis direction cs:0,\offset)}}, Mar 19 at 9:41
  • 1
    This should work for inline tables as well (without having tested it). So transform your coordinates to table, remove the brackets and the +- in the "data" body and give appropriate table headers. Mar 19 at 19:19

1 Answer 1

0

All right, thanks a lot to Stefan, it actually works with inline tables! Here ist the solution:

The key properties are:

visualization depends on={\thisrow{error} \as \offset},
node near coords style={shift={(axis direction cs:\offset,0)}}

Full example:

\documentclass[a4paper, fontsize=12pt]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{/pgf/number format/use comma}

\begin{document}

\begin{tikzpicture} 
\begin{axis}[xbar, bar width=0.7cm, width=0.7\textwidth,
                xmin=0, xmax=7, enlarge y limits = {abs = .8},
                xmajorgrids=true, xminorgrids=true,
                major y tick style={draw=none}, minor x tick num=1,
                minor grid style={dashed},
                ytick={-1,-2}, 
                yticklabels={A,B},
                yticklabel style={align=right},
                nodes near coords,
                visualization depends on={\thisrow{error} \as \offset},
                node near coords style={shift={(axis direction cs:\offset,0)}}
                ]
                
\addplot [fill=red!30, error bars/.cd, x dir=both, x explicit] table [x=value, y=position, x error=error] {
        value   position    error
        4.5     -1          1.197
        3.88    -2          0.995
    };
    
\addplot [fill=green!30, error bars/.cd, x dir=both, x explicit] table [x=value, y=position, x error=error] {
        value   position    error
        5.4     -1          0.7
        4.2     -2          1.508
    };
    
\addplot [fill=yellow!30, error bars/.cd, x dir=both, x explicit] table [x=value, y=position, x error=error] {
        value   position    error
        5.4     -1          0.57
        4.21    -2          0.833
    };
    
\end{axis}
\end{tikzpicture}

\end{document}

Output

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.