我正在尝试使用一些 colorbrewer 配色方案创建面积图。我该怎么做?我在网上找到的东西不知何故不适用于面积图。
这是我的 MWE:
\documentclass[tikz]{standalone}
% package for plots
\usepackage{pgfplots}
\pgfplotsset{
compat=newest,
width=455pt,
height=5cm,
}
% command for plotting a given file
\newcommand{\plotfile}[1]{
\pgfplotstableread[col sep=comma]{#1}{\table}
\pgfplotstablegetcolsof{\table}
\pgfmathtruncatemacro\numberofcols{\pgfplotsretval-1}
\pgfplotsinvokeforeach{1,...,\numberofcols}{
\pgfplotstablegetcolumnnamebyindex{##1}\of{\table}\to{\colname}
\addplot table [y index=##1] {\table}
\closedcycle;
\addlegendentryexpanded{\colname}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=100,
domain=0:100,
ymin=0,
ymax=10000,
xlabel=Generation,
ylabel=Anzahl an Kreaturen,
axis x line*=none,
axis y line*=none,
xtick={0,25,...,100},
ytick={0,1000,...,10000},
tick align=outside,
xticklabel style={
rotate=90,
anchor=near xticklabel
},
legend style={
at={(0.5,1.2)},
legend columns=8,
anchor=south,
draw=none
},
smooth,
stack plots=y,
area style
]
\plotfile{data.csv}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是您可以执行此操作的一种方法。由于您没有提供数据文件,因此我添加了一些虚拟数据。
请注意,我删除了代码中所有不相关的内容。
% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% load the colorbrewer library
\usepgfplotslibrary{colorbrewer}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
stack plots=y,
% load a colorbrewer cycle list
cycle list/Paired,
% things to add to every `\addplot` ...
every axis plot post/.style={
% ... where we add the `fill` which by default is empty
fill=.!50,
% (this is just to show the borders a bit better)
very thick,
},
% adjust the legend style accordingly for the area plots
area legend,
% show the axes on top of the plots
axis on top,
]
% some dummy data table because you didn't provide your data file
\addplot table {
x y1 y2
1 2 3
2 3 4
} \closedcycle;
\addplot table {
x y1 y2
1 2 3
2 3 4
} \closedcycle;
\legend{a,b}
\end{axis}
\end{tikzpicture}
\end{document}