我已经使用 powerpoint 绘制了上面的图形,如何使用 tikz 绘制类似的图形?
我希望每个栏目中有 3 个不同的部分,每个部分应该有 3 种不同的颜色
---------第一个答案后编辑------
我正在使用第一个答案中的代码,但为什么所有的条都重叠了?
\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label First Second Third
10 0.1 0.3 0.3
20 0.2 0.3 0.3
30 0.3 0.4 0.5
40 0.3 0.5 0.8
160 0.5 0.9 1.5
}\datatable
\begin{axis}[
xbar stacked, % Stacked horizontal bars
xmin=0, % Start x axis at 0
ytick=data, % Use as many tick labels as y coordinates
yticklabels from table={\datatable}{Label} % Get the labels from the Label column of the \datatable
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable}; % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}
- - - -编辑 - - -
我更新了我的 pgfplots 包,现在一切都按预期运行!
答案1
您可以为此使用 PGFPlots。它具有xbar stacked
使用堆叠水平条的样式。以下是基于您的图像的示例:
\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label First Second Third
10 0.1 0.3 0.3
20 0.2 0.3 0.3
30 0.3 0.4 0.5
40 0.3 0.5 0.8
160 0.5 0.9 1.5
}\datatable
\begin{axis}[
xbar stacked, % Stacked horizontal bars
xmin=0, % Start x axis at 0
ytick=data, % Use as many tick labels as y coordinates
yticklabels from table={\datatable}{Label} % Get the labels from the Label column of the \datatable
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable}; % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}