条形图-PGFlots

条形图-PGFlots

首先,抱歉我的英语不好。我想知道如何制作类似图的条形图。我尝试使用此代码 PGFPlots 包,但效果不佳。有人知道怎么做吗?

\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

答案1

或者,如果由于某种原因您没有最新版本的 PGFplots,您可以使用 tikz 放入一些节点,但这非常混乱。

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

答案2

使用当前版本的 PGFPlots (1.12),标签会自动放置在条形图内。

nodes near coords您可以通过设置来更改的颜色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}

相关内容