16

I am trying to plot explicit error bars like this:

\begin{tikzpicture} 
\begin{axis} [symbolic x coords={Lighting,Computers,Total}]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
    (Lighting,0.12) +- (0.31,0.03)
    (Computers,0.06) +- (0.12,0.01) 
    (Total,0.07) +- (0.14,0.02)
};
\end{axis} 
\end{tikzpicture}

Except this produces a constant error instead of the explicit values I have provided. For example, for Lighting, it produces 0.12 +-0.03 (see below). Did I misread the manual?! There is also some weirdness with the labels on the x-axis and I'd rather not have the y-axis use exponential notation for y<0.1. (I tried posting the output, but apparently I need more cred!)

4
  • 3
    Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It is considered a lot better to put in some code that will compile, as it makes it a lot easier for us to copy it into our text editor and work with it, and see exactly what it is you are trying to do. Nov 6, 2012 at 22:08
  • As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again.
    – hpesoj626
    Nov 7, 2012 at 1:54
  • @hpesoj626 the OP isn't reporting an error. The code runs OK for him but the error bars don't produce the expected behaviour. Nov 7, 2012 at 2:31
  • @GonzaloMedina An MWE might help solvers but I guess you already have the answer :)
    – hpesoj626
    Nov 7, 2012 at 2:34

3 Answers 3

9

Starting from PGFPlots version 1.9, asymmetric error bars are directly supported:

\documentclass{article} 
\usepackage{pgfplots}

\pgfplotstableread{
x         y    y-max  y-min
Lighting  0.12 0.31   0.03
Computers 0.06 0.12   0.01 
Total     0.07 0.14   0.02
}{\mytable}

\begin{document}    
\begin{tikzpicture}
\begin{axis} [
    ymin=0,
    symbolic x coords={Lighting,Computers,Total},
    xtick=data
]
\addplot [only marks] 
  plot [error bars/.cd, y dir=both, y explicit]
  table [y error plus=y-max, y error minus=y-min] {\mytable};
\end{axis} 
\end{tikzpicture}

\end{document}

8

Update

From version 1.12.1 of pgfplots (at least), asymmetric error values are supported through y error plus, and y error minus:

\documentclass{article} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotstableread{
x         y    y-max  y-min
Lighting  0.12 0.31   0.03
Computers 0.06 0.12   0.01 
Total     0.07 0.14   0.02
}{\mytable}

\begin{document}

\begin{tikzpicture}[scale=1.3] 
\begin{axis} [symbolic x coords={Lighting,Computers,Total},xtick=data]
\addplot+[forget plot,only marks] 
  plot[error bars/.cd, y dir=both, y explicit]
  table[x=x,y=y,y error plus expr=\thisrow{y-max},y error minus expr=\thisrow{y-min}] {\mytable};
\end{axis} 
\end{tikzpicture}

\end{document}

Old versions of pgfplots

I read the documentation for pgfplots but, as far as I could see, asymmetric error bars are not supported (error bars will use one same value); a possible workaround is to use two addplots: one for the upper error values, and the other one for the lower error values; in the following example I used \pgfplotstableread from the pgfplotstable package to store the data and easily reuse it. Here's a little example:

\documentclass{article} 
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotstableread{
x         y    y-max  y-min
Lighting  0.12 0.31   0.03
Computers 0.06 0.12   0.01 
Total     0.07 0.14   0.02
}{\mytable}

\begin{document}

\begin{tikzpicture}[scale=1.3] 
\begin{axis} [symbolic x coords={Lighting,Computers,Total},xtick=data]
\addplot+[forget plot,only marks] 
  plot[error bars/.cd, y dir=plus, y explicit]
  table[x=x,y=y,y error expr=\thisrow{y-max}] {\mytable};
\addplot+[only marks,xticklabels=\empty] 
  plot[error bars/.cd, y dir=minus, y explicit]
  table[x=x,y=y,y error expr=\thisrow{y-min}] {\mytable};
\end{axis} 
\end{tikzpicture}

\end{document}

enter image description here

0
4

Below is my try at guessing what your problem is. From the comments, it is always a good practice to include a fully-compilable MWE.

\documentclass[10pt]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}

\begin{document}
\begin{tikzpicture}
\begin{axis} [%log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total}]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
    (Lighting,0.12) +- (0.31,0.03)
    (Computers,0.06) +- (0.12,0.01) 
    (Total,0.07) +- (0.14,0.02)
};
\end{axis} 
\end{tikzpicture}

\end{document}

Here is the output. (Though I still encourage to post your picture so that we can see what's really happening in your actual case.)

enter image description here

Note the repeated labels.

To resolve this, I manually set the width smaller. (I think the default is 7 cm.) Here, I used width=6.5cm.

\begin{tikzpicture}
\begin{axis} [%log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total},width=6.5cm]% Set the width smaller
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
    (Lighting,0.12) +- (0.31,0.03)
    (Computers,0.06) +- (0.12,0.01) 
    (Total,0.07) +- (0.14,0.02)
};
\end{axis} 
\end{tikzpicture}

Here is what I get.

enter image description here

One workaround to remove exponentiation in the y-axis is to use the {semilogyaxis} with log ticks with fixed point option.

\begin{tikzpicture}
\begin{semilogyaxis} [log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total},width=6.5cm]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
    (Lighting,0.12) +- (0.31,0.03)
    (Computers,0.06) +- (0.12,0.01) 
    (Total,0.07) +- (0.14,0.02)
};
\end{semilogyaxis} 
\end{tikzpicture}

Here is the output:

enter image description here

Edit

Here is another code snippet using the xtick=data,ytick=ydata option.

\begin{tikzpicture}
\begin{semilogyaxis} [log ticks with fixed point
,symbolic x coords={Lighting,Computers,Total},xtick=data,ytick=data]
\addplot+[only marks] plot[error bars/.cd, y dir=both, y explicit]
coordinates{
    (Lighting,0.12) +- (0.31,0.03)
    (Computers,0.06) +- (0.12,0.01) 
    (Total,0.07) +- (0.14,0.02)
};
\end{semilogyaxis} 
\end{tikzpicture}

enter image description here

You must log in to answer this question.

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