1

Looking to position the symbolic x tick labels above the bars. Also, wondering why the last bar isn't showing correctly.

% Minimum working example
\documentclass[varwidth=false, tikz, border=10pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{filecontents*}{data.dat}
Num Funding Revenue Cost    Margin  Percent
1   22000   22000   18261   3739    17
2   18400   18400   9283    9117    50
3   12338   12338   3929    8409    68
4   11350   11350   4859    6491    57
5   11312   11312   3514    7797    69
\end{filecontents*}

\pgfplotstableread{data.dat}\dataA

\pgfplotsset{compat=newest}
\definecolor{jhcobalta}{RGB}{0,59,92}
\definecolor{jhcoppera}{RGB}{229,114,0}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar stacked, 
legend style={at={(0.5,-0.12)},
      anchor=north,legend columns=-1},
xmin=0,xmax=5,
bar width=3mm,
xtick=data,
xticklabels from table={\dataA}{Percent},
axis lines = middle,
enlargelimits = {abs=1},
ylabel={\$},
y label style={anchor=south},
yticklabel pos=top,
scaled ticks=false,
axis x line*=middle,
axis y line*=left,
x tick label style={anchor=north, align=center},
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
},
x tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=0,
/tikz/.cd
},
point meta=explicit symbolic,
nodes near coords,% align={vertical},
tick label style={/pgf/number format/assume math mode}
]
\addplot+[ybar,fill=jhcobalta,restrict x to domain=1:45] table [x=Num, y=Cost] {\dataA};\addlegendentry{Cost}
\addplot+[ybar,fill=jhcoppera,restrict x to domain=1:45] table [x=Num, y=Margin] {\dataA};\addlegendentry{Margin}

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    Regarding the second question, look at which value the y-axis starts. (And then add ymin=0.) Commented Jul 13, 2019 at 19:53

1 Answer 1

3

As was already stated by Torbjørn T. in the comment below the question adding ymin=0 solves your second question. The default ymin value is chosen higher than the y value of the last bar, thus it cannot be seen.

Regarding the first question you have to adapt the nodes near coords style thus that the nodes near coords value is shifted half the rawy value (plus some other minor stuff.)

Please note that I also have heavily simplified the rest of your code.

    \begin{filecontents*}{data.dat}
        Num Funding Revenue Cost    Margin  Percent
        1   22000   22000   18261   3739    17
        2   18400   18400   9283    9117    50
        3   12338   12338   3929    8409    68
        4   11350   11350   4859    6491    57
        5   11312   11312   3514    7797    69
    \end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplotstable}
    \definecolor{jhcobalta}{RGB}{0,59,92}
    \definecolor{jhcoppera}{RGB}{229,114,0}
    \pgfplotsset{compat=newest}
    \pgfplotstableread{data.dat}\dataA
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        bar width=3mm,
        ymin=0,             % <-- added
        xtick=data,
        xticklabels from table={\dataA}{Percent},
        axis lines=middle,
        axis y line*=left,
        enlargelimits={abs=1},
        ylabel={\$},
        y label style={
            anchor=south,
        },
        scaled ticks=false,
        tick label style={
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision=0,
                assume math mode,
            /tikz/.cd,
        },
        x tick label style={
            anchor=north,
            align=center,
        },
        legend style={
            at={(0.5,-0.12)},
            anchor=north,
            legend columns=-1,
        },
        enlarge x limits=0.15,
        % ---------------------------------------------------------------------
        % added or modified
        % (adapted from <https://tex.stackexchange.com/a/497635/95441>
        visualization depends on=rawy \as \rawy,
        point meta=\thisrow{Percent},
        nodes near coords style={
            black,
            font=\tiny,
            shift={(axis direction cs:0,\rawy/2)},
            anchor=south,
        },
        % ---------------------------------------------------------------------
        table/x=Num,
    ]
        \addplot+ [jhcobalta] table [y=Cost] {\dataA};
        \addplot+ [
            jhcoppera,
            nodes near coords,
       ] table [y=Margin] {\dataA};

        \legend{
            Cost,
            Margin,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

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