3

I would like to add horizontal error bars to my plot, pictured below. enter image description here The code that generates the above plot is:

\begin{tikzpicture}[thick,scale=1, every node/.style={transform shape},trim axis left, trim axis right]
\pgfplotsset{width=16cm,
    compat=1.3,
    legend style={font=\footnotesize}}
\begin{axis}[
xlabel={ $1/r^2$[\SI{}{1\per\centi\square\meter}]},
ylabel={$i$[\SI{}{\micro\ampere}]},
%extra x ticks={3.5},
%extra tick style={grid=major,major grid style={red,thick},
%tick label style={
%rotate=90,anchor=east}},
%extra x tick labels={Actual},
legend cell align=left,
legend pos=north west],
\addplot[only marks,
        error bars/.cd,
            y dir=both,
            y explicit,
            every nth mark=1
    ] table[x=X,y=Y,y error=Z]{
    X Y Z
0.01000 56.0    2
0.008264    41.0    3
0.006944    31.0    4
0.005917    24.0    5
0.005102    19.0    6
0.004444    15.5    7
0.003906    12.5    9
0.003460    10.5    9
0.003086    9.0     3
0.002770    8.0     2
};
\addlegendentry{Punkty pomiarowe} %
 %\draw[ultra thin] (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymax});  
\end{axis}
\end{tikzpicture} \\

And the function that creates the error bars is:

\newcounter{marknumber}
\pgfplotsset{
error bars/every nth mark/.style={
    /pgfplots/error bars/draw error bar/.prefix code={
        \pgfmathtruncatemacro\marknumbercheck{mod(floor(\themarknumber/2),#1)}
        \ifnum\marknumbercheck=0
        \else
            \begin{scope}[opacity=0]
        \fi
    },
    /pgfplots/error bars/draw error bar/.append code={
        \ifnum\marknumbercheck=0
        \else
            \end{scope}
        \fi
        \stepcounter{marknumber}    
    }
}
}
3

it's not clear what your question is (I guess it's in the title, isn't it?). I guess you wnat to add horizontal error bars, that is: in addition to the vertical ones that already are in the plot.

I think that the following might be what you are looking for:

enter image description here

\documentclass{minimal}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}
\addplot+[error bars/.cd,
x dir=both,x explicit relative,
y dir=both,y explicit relative,
]
table[x=x,y=y,x error=xerror,y error=yerror]
{
x      y        xerror  yerror
32     32       0       0.1
64     64       0       0.5
128    128      0.3     0.25
1024   1024     0.2     0.12
32068  32068    0.6     0.4
64000  64000    0.6     0.26
128000 128000   0.6     0.16
};
\end{loglogaxis}
\end{tikzpicture}
\end{document}

You can add both x and y error bars by using

\addplot+[error bars/.cd,
x dir=both,x explicit relative,
y dir=both,y explicit relative,
]

Otherwise, if you just want to switch the vertical error bars to horizontal ones, you can just simply write:

table[x=X,y=Y,x error=Z]
| improve this answer | |

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.