1

Here are my MWE:

\documentclass{standalone}

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

\begin{document}

\pgfplotstableread{
x y error 
P53     0.21    0.07 
IgG    -0.16    0.06 
cTnI    0.03    0.04 
PSA    -0.22    0.05 
Myo    -0.05    0.03 
AFP    -0.09    0.02 
Serum   0.05    0.06 
CKMB    0.89    0.01 
}\datatable

\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=14pt,
    symbolic x coords={P53,IgG,cTnI,PSA,Myo,AFP,Serum,CKMB},
    xlabel=xlabel,
    ylabel=xlabel,
    ]
    \addplot+ [   
    error bars/.cd,
    y dir=both,
    y explicit relative, 
    ] table [y error=error] {\datatable};
    %\addplot+ (P53,0) -- (CKMB,0);
  \end{axis}
\end{tikzpicture}


\end{document}

And here is my expected result:

enter image description here

PS: Can you tell me what's wrong with the x-tick?

2
  • I'm confused. It seems you are not getting the red axis line, and you want that. Is that correct? If so, instead of killing yourself with the intricacies of pgfplots, why not just a \draw[red] ... after the \end{axis}, and adjust the cordinates by hand? Not pretty, but less time with TeX means more time with ... – Augustin Dec 24 '20 at 15:41
  • It should be \addplot+ coordinates {(P53,0) (CKMB,0);} or just \draw[red] (P53,0) -- (CKMB,0);. – user231225 Dec 24 '20 at 16:02
1

With use of the extra x tick is simple:

\documentclass[margin=3.14159]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}

\pgfplotstableread{%
x       y       error
P53     0.21    0.07
IgG    -0.16    0.06
cTnI    0.03    0.04
PSA    -0.22    0.05
Myo    -0.05    0.03
AFP    -0.09    0.02
Serum   0.05    0.06
CKMB    0.89    0.01
    }\datatable

    \begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=14pt,
    symbolic x coords={P53,IgG,cTnI,PSA,Myo,AFP,Serum,CKMB},
    xlabel=xlabel,
    ylabel=ylabel,
    extra y ticks=(0),  % <---
    extra y tick style={grid, % <---
                        grid style={thick, red}},
            ]
\addplot + [error bars/.cd,
            y dir=both, y explicit relative,
            ] table [y error=error] {\datatable};
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1

I suspect that pgfplots only used every third bar in order to fit the x tick labels. OTOH, reducing the font size by itself was not sufficient.

The left and right sides are best defined using (rel axis cs:) while the data points are defined using (axis cs:). It was easier to define named coordinates first then find the perpendiculars: (sw |- zero) and (ne |- zero).

\documentclass{standalone}

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

\begin{document}

\pgfplotstableread{
x y error 
P53     0.21    0.07 
IgG    -0.16    0.06 
cTnI    0.03    0.04 
PSA    -0.22    0.05 
Myo    -0.05    0.03 
AFP    -0.09    0.02 
Serum   0.05    0.06 
CKMB    0.89    0.01 
}\datatable

\begin{tikzpicture}
\begin{axis}[
    ybar,
    bar width=14pt,
    symbolic x coords={P53,IgG,cTnI,PSA,Myo,AFP,Serum,CKMB},
    xlabel=xlabel,
    ylabel=ylabel,
    xtick=data,
    x tick label style={font={\tiny}}
    ]
    \addplot+ [   
    error bars/.cd,
    y dir=both,
    y explicit relative, 
    ] table [y error=error] {\datatable};
    %\addplot+ (P53,0) -- (CKMB,0);
    \coordinate (zero) at (axis cs: P53,0);
    \coordinate (sw) at (rel axis cs: 0,0);
    \coordinate (ne) at (rel axis cs: 1,1);
  \end{axis}
  \draw[red] (sw |- zero) -- (ne |- zero);
\end{tikzpicture}
\end{document}

demo

1

There are several ways to add the horizontal line. I think the simplest way to add the line is using an extra y tick, because this works independently of the data.

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
        \pgfplotstableread{
            x      y        error
            P53     0.21    0.07
            IgG    -0.16    0.06
            cTnI    0.03    0.04
            PSA    -0.22    0.05
            Myo    -0.05    0.03
            AFP    -0.09    0.02
            Serum   0.05    0.06
            CKMB    0.89    0.01
        }\datatable
    \begin{axis}[
        ybar,
        bar width=14pt,
        xlabel=xlabel,
        ylabel=ylabel,
%        % --------------------------------------------------------------------
%        % (much simpler than using `symbolic coords` is ...
%        symbolic x coords={P53,IgG,cTnI,PSA,Myo,AFP,Serum,CKMB},
        table/x expr=\coordindex,
        xtick=data,
        xticklabels from table={\datatable}{x},
        xticklabel style={
            rotate=90,
        },
        % ... By that you don't have to repeat yourself if "x values" change.)
        % ---------------------------------------------------------------------
        % extra line at $y = 0$
        extra y ticks={0},
        extra y tick labels={},
        extra y tick style={
            grid=major,
            grid style={
                red,
            },
        },
    ]
        \addplot+ [
            error bars/.cd,
                y dir=both,
                y explicit relative,
        ] table [y error=error] {\datatable};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1
  • Thanks! It works for me! – go go Dec 25 '20 at 1:48

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.