1

I have investigated this a couple of hours now, and the best I found was Define error bar color in scatter class. Too bad, the solution doesn't compile for me.

I want my error bars to use the same color from a colormap as the marks use. Below, if the error bars overlap the zero line they should be somewhat transparent; otherwise, they should be solid.

I have managed to make the marks changes as I want them to, but I'm at a loss for the error bars. Cheers!

MWE:

enter image description here

\documentclass[border=5mm]{standalone}      
\usepackage{pgfplots}  
\usepackage{pgfplotstable}  
\usepackage{filecontents}  
\pgfplotsset{compat=1.17}

\begin{document}


\begin{filecontents}{testdata.csv}  
value, err  
-1.0,   0.5  
-0.5,   2  
 1.1,   3  
 2.5,   2  
 3.1,   4  
 3.5,   2 
 4.0,   5  
\end{filecontents}

\pgfplotstableread[col sep=comma]{testdata.csv}\testtable  
\begin{tikzpicture}  
\begin{axis}[  
    colormap={greens}{color=(green!10) color=(green!100)}  
    ]  
    \draw [dashed] (axis cs: 0,\pgfkeysvalueof{/pgfplots/ymin}) --   
                   (axis cs: 0,\pgfkeysvalueof{/pgfplots/ymax});  
    \addplot+ [scatter,  
              only marks,  
              scatter src=(abs(\thisrow{value})-\thisrow{err}>0),  
              error bars/.cd,  
                x explicit,  
                x dir=both  
            ] table [  
                x=value,  
                y expr=\coordindex,  
                x error expr=\thisrow{err}  
            ] {\testtable};  
\end{axis}  
\end{tikzpicture}  
\end{document}
1

1 Answer 1

1

By using https://tex.stackexchange.com/a/34681 which TikZling suggested I was able to modify my code as follows, to get the desired result. I also removed some, for my situation, superfluous code and added axis cs: to not have to manually rescale the error bars.

I have not been able to make the code dynamic enough to set xmin and xmax automatically to fit the error bars.

enter image description here

Solution:

\documentclass[border=5mm]{standalone} 
\usepackage{pgfplots}  
\usepackage{pgfplotstable}  
\usepackage{filecontents}  
\pgfplotsset{compat=1.17}  
  
\pgfplotsset{  
    error bars with mapped color/.style={  
        disabledatascaling,  
        visualization depends on=\thisrow{err} \as \error,  
        scatter/@pre marker code/.append style={  
            /pgfplots/error bars/.cd,  
            error mark options={draw=mapped color},  
            error mark=|,  
            draw error bar={(0,0)}{(\error,0)},  
            draw error bar={(0,0)}{(-\error,0)}  
        }  
    }  
}  

\begin{document}  


\begin{filecontents}{testdata.csv}  
value, err  
-1.0,   0.5  
-0.5,   2  
 1.1,   3  
 2.5,   2  
 3.1,   4  
 3.5,   2  
 4.0,   5  
\end{filecontents}  

\pgfplotstableread[col sep=comma]{testdata.csv}\testtable  
\begin{tikzpicture}  
\begin{axis}[  
    colormap={greens}{color=(green!20) color=(green!100)},  
    disabledatascaling,  
    scatter/use mapped color={  
        draw=mapped color, fill=mapped color},  
    error bars with mapped color/.style={  
        visualization depends on=\thisrow{err} \as \error,  
        scatter/@pre marker code/.append style={  
            /pgfplots/error bars/.cd,  
            error mark options={draw=mapped color},  
            error mark=|,  
            draw error bar={(0,0)}{(axis cs: \error,0)},  
            draw error bar={(0,0)}{(axis cs: -\error,0)}  
        },  
        scatter/@post marker code/.append code={}  
    },  
    xmin=-2.9,  
    xmax=10.1]  
    \draw [dashed] (axis cs: 0,\pgfkeysvalueof{/pgfplots/ymin}) --   
                   (axis cs: 0,\pgfkeysvalueof{/pgfplots/ymax});  
    \addplot [scatter,  
    scatter src=(abs(\thisrow{value})-\thisrow{err}>0),  
    only marks,  
    error bars with mapped color=err,  
    error bars/.cd,  
        x dir=both,  
        x explicit  
    ] table [  
    x=value, y expr=\coordindex]  
    {\testtable};  
\end{axis}  
\end{tikzpicture}  
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .