条形图标签和网格

条形图标签和网格
\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}

答案1

为了绘制线条y=0,我会使用

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

乍一看有点吓人,但其实没那么糟糕。rel axis cs=0,0是绘图区域的左下角,rel axis cs:1,0是右下角。axis cs:0,0是坐标系的原点。语法指定位于一条垂直线和一条水平线(A|-B)的交点上的点,所以是位于坐标系原点高度的绘图区域左下角上方的点。括号是必要的,以掩盖逗号,逗号通常将不同的选项彼此分开。AB({rel axis cs:0,0}|-{axis cs:0,0})

与说相比,这种方法的优点\draw (axis cs:0,0) -- (axis cs:10,0)是,无论范围是多少,线总是会跨越整个图的宽度x

为了获取标签,我建议使用nodes near coords。它们在每个绘图坐标处放置一个节点,默认情况下打印值meta,如果不可用,则打印y值。对于处理像您这样的相当复杂的数据集(坐标、错误和元数据),我建议使用\addplot table而不是\addplot coordinates,因为它的灵活性要差得多。

您只需nodes near coords, point meta=explicit在选项中说addplot,并meta通过在选项meta=<column name>中说来指定源table,这将在每个坐标处添加带有标签值的节点。但是,对于负值y,这会导致节点最终位于条形图内,而不是条形图下方,因为垂直位置取决于数据的符号meta。要解决这个问题,您可以使用稍微高级一点的功能,visualization depends on = \thisrow{<column name>} \as <macro>这会在内部提供一个额外的表格列nodes near coords。如果您说nodes near coords=\pgfmathprintnumber{<macro>},垂直位置将由值决定y,但节点文本将取决于表中的不同列。

\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

要加粗轴线,你可以简单地在它上面绘制:

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

您可以使用\node来放置文本:

在此处输入图片描述

\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}

相关内容