5

first sorry for my English. I wonder how to make a bar graph like figure. I tried with this code PGFPlots package, but is not good. Someone knows how to do this?

\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    bar width=45pt,
    nodes near coords,
    enlargelimits=0.22,
    legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
    xtick={1, 2, 3},
    xticklabels={x1, x2, x3},
    ]

\addplot+[ybar, color=green] plot
    coordinates {
    (1,4096) (2,4096) (3,4096)
    };

\addplot+[ybar, color=red] plot
    coordinates {
    (1,4096) (2,12288) (3,20480)
    };

\legend{\strut Bits O., \strut Bits E.}
\end{axis}
\end{tikzpicture}

PGFPlots

2

Alternatively, if for some reason you don't have the latest version of PGFplots you can put some nodes in with tikz but this is very messy.

\documentclass[border=10pt,varwidth]{standalone}
\usepackage[usernames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{PGFplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=45pt,
enlargelimits=0.3,
legend style={at={(0.29,-0.075)},
{/tikz/every even column/.append style={column sep=0.5cm}},
draw=none,
anchor=north, legend columns=0},
symbolic x coords={x1, x2, x3},
xtick={data},
xtick pos=left,
ytick pos=left,
ymajorgrids,
scaled y ticks = false,
ytick={0,2000,4000,6000,8000,10000,12000,14000,16000,18000},
axis line style={Gray},
every tick/.append style={Gray},
]

\addplot+[ybar, color=Green] plot
coordinates {
(x1,4096) 
(x2,4096) 
(x3,4096) 
};

\addplot+[ybar, color=Red] plot
coordinates {
(x1,4096)
(x2,8192)
(x3,12288)
};

\coordinate (A) at (0.4,-20);
\coordinate (B) at (0.4,20);
\coordinate (C) at (100,-20);
\coordinate (D) at (100,40);
\coordinate (E) at (200,-20);
\coordinate (F) at (200,60);


\legend{Bits O, Bits E}
\end{axis}
\node at (A) {\color{white}\textbf{4096}};
\node at (B) {\color{white}\textbf{4096}};
\node at (C) {\color{white}\textbf{4096}};
\node at (D) {\color{white}\textbf{8192}};
\node at (E) {\color{white}\textbf{4096}};
\node at (F) {\color{white}\textbf{12288}};
\end{tikzpicture}

\end{document}
| improve this answer | |
6

With the current version of PGFPlots (1.12), the labels are placed inside the bars automatically.

You can change the color of the nodes near coords by setting every node near coord/.style=black.

\documentclass[11pt]{article}

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    bar width=45pt,
    nodes near coords,
    enlarge x limits=0.25,
    ymin=0,
    legend style={
        at={(0.5,-0.20)},
        anchor=north,
        legend columns=-1},
    xtick={data},
    xticklabels={x1, x2, x3},
    scaled y ticks=false,
    axis on top,
    /pgf/number format/1000 sep=\,
    ]

\addplot+[color=cyan!70, every node near coord/.style=black] plot
    coordinates {
    (1,4096) (2,4096) (3,4096)
    };

\addplot+[orange!80, every node near coord/.style=black] plot
    coordinates {
    (1,4096) (2,12288) (3,20480)
    };

\legend{\strut Bits O., \strut Bits E.}
\end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |

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.