# Scale error bars with y bar

I scaled the y bar is in example:

How to scale existing coordinates data in pgfplots?

the only problem is, that my error bar stays unchanged. Hence it's size is totally wrong.

Hi can I scale the error accordingly to the y bar?

Here my example:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
ymin=0,
symbolic x coords={one,two,three, four},
xtick=data,
ylabel={$y$},
y filter/.code={\pgfmathparse{#1*1000}\pgfmathresult},
]
\addplot[ybar, fill=red!30, error bars/error bar style={red}]
plot [error bars/.cd, y dir=both, y explicit, error mark options={rotate=90,mark size=4pt}] coordinates
{(one,0.00981)+-(one,0.00002)
(two,0.00482)+-(two,0.00002)
(three,0.00478)+-(three,0.00001)
(four,0.01003)+-(four,0.00003)};
\end{axis}
\end{tikzpicture}
\end{document}

• Solving this is nontrivial because the error bars are drawn with plain TikZ and you will easily run into dimension too large errors if you apply your multiplication by 1000. The much cleaner way IMHO would be to just scale the y coordinates in the input. – user194579 Aug 15 '19 at 1:24

With units library you can achieve this by using:

    change y base,
y SI prefix=milli,


Note that I have increased the error value (10x) so that it will be visible.

### LaTeX source

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{units}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
title=no scaling,
ymin=0,
symbolic x coords={one,two,three, four},
xtick=data,
ylabel={$y$},
%y filter/.code={\pgfmathparse{#1*1000}\pgfmathresult},
]
\addplot[ybar, fill=red!30, error bars/error bar style={red}]
plot [error bars/.cd, y dir=both, y explicit, error mark options={rotate=90,mark size=4pt}] coordinates
{(one,0.00981)+-(one,0.0002)
(two,0.00482)+-(two,0.0002)
(three,0.00478)+-(three,0.0001)
(four,0.01003)+-(four,0.0003)};
\end{axis}
\end{tikzpicture}%
\begin{tikzpicture}
\begin{axis} [
title=after scaling,
ymin=0,
symbolic x coords={one,two,three, four},
xtick=data,
ylabel={$y$},
%y filter/.code={\pgfmathparse{#1*1000}\pgfmathresult},
change y base,
y SI prefix=milli,
]
\addplot[ybar, fill=red!30, error bars/error bar style={red}]
plot [error bars/.cd, y dir=both, y explicit, error mark options={rotate=90,mark size=4pt}] coordinates
{(one,0.00981)+-(one,0.0002)
(two,0.00482)+-(two,0.0002)
(three,0.00478)+-(three,0.0001)
(four,0.01003)+-(four,0.0003)};
\end{axis}
\end{tikzpicture}
\end{document}


I personally don't like stating coordinates to the \addplot command, because this is much harder to write as a table and also doesn't allow easy manipulation of the data. So I converted your coordinates to a table and then applied the scaling of the values to both, i.e. the y values and the y error values.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0,
symbolic x coords={one,two,three, four},
xtick=data,
ylabel={$y$},
]
ybar,
fill=red!30,
error bars/error bar style={red},
error bars/.cd,
y dir=both,
y explicit,
error mark options={
rotate=90,
mark size=4pt,
},
] table [
x=x,
y expr={1000*\thisrow{y}},
y error expr={1000*\thisrow{yerr}},
] {
% scaled by value of 10 so they can be seen
x       y        yerr
one     0.00981  0.0002
two     0.00482  0.0002
three   0.00478  0.0001
four    0.01003  0.0003
};
\end{axis}
\end{tikzpicture}
\end{document}


In my opinion, that figure can be drawn with plain TikZ. We can change scaling as we wish.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[yscale=.5,xscale=1.7]
\foreach \i/\itext/\ivalue in
{1/one/9.81,2/two/4.82,3/three/4.78,4/four/10.03}
\draw[blue,line width=4mm]
(\i,0) node[below,black]{\itext}--+(90:\ivalue);
\foreach \j in {0,2,...,10}
\draw
(.5,\j) node[left]{\j}--+(0:1mm)
(4.5,\j)--+(180:1mm);
\draw (.5,0) rectangle (4.5,11);
\path (current bounding box.west) node[left=5mm,rotate=90]{$y$};
\end{tikzpicture}
\end{document}