6
\documentclass[a4paper,11pt]{report}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title = {Optimalization based upon co-occurences},
xbar,
width=10cm,
xtick={1,...,8},
xticklabels={%
    atpB,
    atpE,
    atpF,
    atpH,
    atpA,
    atpG,
    atpD,
        atpC},
    grid=major,
    ]
\draw [red, ultra thick] (axis cs:0,0) -- (axis cs:10,0);
\addplot[fill=blue,draw=black,ybar,error bars/.cd,y dir=both,y explicit] 
coordinates
{(1,-0.279535)  +- (0.015982,0.015982)
(2,0.739360)    +- (0.031211,0.031211)
(3,-0.279302)   +- (0.017384,0.017384)
(4,-0.602794)   +- (0.022327,0.022327)
(5,0.487714)    +- (0.015970,0.015970)
(6,-0.294501)   +- (0.014923,0.014923)
(7,0.526527)    +- (0.016725,0.016725)
(8,-0.297469)   +- (0.021122,0.021122)
};
\end{axis}
\end{tikzpicture}
\end{document}
5
  • 1
    Welcome to TeX.sx! It's nice if you make your example complete, with a documentclass and the necessary packages. That way we can just copy-paste the code and compile it directly. Nov 22, 2011 at 10:18
  • You could use something like \draw [red, ultra thick] (axis cs:0,0) -- (axis cs:10,0); to thicken the x-axis. Nov 22, 2011 at 10:31
  • If you have the answer, could you post it? It's fine to answer your own questions.
    – qubyte
    Nov 22, 2011 at 10:46
  • Well it is only solved for the 0 line and it is added. I am still struggeling to get numbers above my bars but they should not correspond to the height but to a different value.
    – Jasper
    Nov 22, 2011 at 11:04
  • 1
    @Jasper: It might be a good idea to edit the actual question back in, otherwise Peter's and my answers don't make much sense to other people.
    – Jake
    Nov 22, 2011 at 18:58

2 Answers 2

7

For drawing the line at y=0, I would use

\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});

It looks a bit intimidating at first, but it's not actually that bad. rel axis cs=0,0 is the lower left corner of the plot area, rel axis cs:1,0 is the lower right. axis cs:0,0 is the origin of the coordinate system. The syntax (A|-B) specifies the point that lies on the intersection of a vertical line through A and a horizontal line through B, so ({rel axis cs:0,0}|-{axis cs:0,0}) is the point above the lower left corner of the plot area at the height of the coordinate system origin. The braces are necessary to mask the comma, which usually separates different options from each other.

The advantage of this approach compared to saying \draw (axis cs:0,0) -- (axis cs:10,0) is that the line will always span the entire width of the plot, no matter what the x range is.

For getting the labels, I would suggest using nodes near coords. These place a node at each plot coordinate, by default printing the meta value, or if that is not available, the y value. For working with reasonably complex datasets like yours (coordinates, error and meta data), I would recommend using \addplot table instead of \addplot coordinates, which is much less flexible.

You could just say nodes near coords, point meta=explicit in the addplot options, and specify the meta source by saying meta=<column name> in the table options, which would add nodes with the label values at each coordinate. However, for negative y values, this would cause the nodes to end up inside the bar, instead of underneath them, because the vertical placement depends on the sign of the meta data. To work around this, you can use a slightly more advanced feature, visualization depends on = \thisrow{<column name>} \as <macro>, which makes an additional table column available inside the nodes near coords. If you then say nodes near coords=\pgfmathprintnumber{<macro>}, the vertical placement will be determined by the y value, but the node text will depend on a different column in your table.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}


\begin{tikzpicture}
\begin{axis}[
    title = {Optimization based upon co-occurences},
    xbar,
    width=10cm,
    xtick={1,...,8},
    xticklabels={%
        atpB,
        atpE,
        atpF,
        atpH,
        atpA,
        atpG,
        atpD,
        atpC},
    grid=major,
    ]

\addplot[
    fill=blue!25,
    draw=black,
    ybar,
    point meta=y,
    visualization depends on=\thisrow{label} \as \barlabel,
    nodes near coords=\pgfmathprintnumber{\barlabel},
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error] {
x   y           error    label
1   -0.279535   0.015982 2 
2   0.739360    0.031211 4
3   -0.279302   0.017384 1 
4   -0.602794   0.022327 1 
5   0.487714    0.015970 8 
6   -0.294501   0.014923 4 
7   0.526527    0.016725 5 
8   -0.297469   0.021122 1
};

\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}


\end{document}
2
  • Hmm somehow the computer keeps building but it never makes the graph. No error's are produced as it is continuously processing it.
    – Jasper
    Nov 22, 2011 at 14:11
  • Maybe you're using an old version of PGFplots (current is 1.5) or TikZ (current is 2.1). Have you tried updating those two packages?
    – Jake
    Nov 22, 2011 at 18:56
2

To thicken the axis line you could simply draw over it:

\draw [red, ultra thick] (axis cs:0,0) -- (axis cs:10,0);

You could use \node to place the text:

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title = {Optimalization based upon co-occurences},
    xbar,
    width=10cm,
    xtick={1,...,8},
    xticklabels={%
        atpB,
        atpE,
        atpF,
        atpH,
        atpA,
        atpG,
        atpD,
        atpC},
    grid=major,
    ]

\addplot[fill=blue,draw=black,ybar,error bars/.cd,y dir=both,y explicit] 
coordinates
{(1,-0.279535)  +- (0.015982,0.015982)
(2,0.739360)    +- (0.031211,0.031211)
(3,-0.279302)   +- (0.017384,0.017384)
(4,-0.602794)   +- (0.022327,0.022327)
(5,0.487714)    +- (0.015970,0.015970)
(6,-0.294501)   +- (0.014923,0.014923)
(7,0.526527)    +- (0.016725,0.016725)
(8,-0.297469)   +- (0.021122,0.021122)
};

\draw [red, ultra thick] (axis cs:0,0) -- (axis cs:10,0);
\node [below, yshift=-0.5ex] at (axis cs:1,-0.279535) {A};
\node [above, yshift=0.5ex ] at (axis cs:2,0.739360) {B};
\end{axis}
\end{tikzpicture}
\end{document}
0

You must log in to answer this question.

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