我正在尝试在同一张图上绘制以一个 x 轴与多个 y 轴的形式出现的数据。我希望每条线的颜色能够通过我想要的颜色图(光谱着色)自行展开。
我想要绘制的数据示例可以在这里找到:http://pastebin.com/R8nC0zq4
如果我将此数据集复制到 MATLAB 中,我可以设置我的图来显示我希望它最终在 pgfplots 中显示的示例。考虑复制到的 pastebin 代码MATLAB
,该代码位于名为的变量中data
:
cmp = jet(10);
hLine = plot(data(:,1),data(:,2:end)); %plot x vs all the y columns (10)
for line = 1:10
set(hLine(line),'Color',cmp(line,:));
end
set(hLine,'LineWidth',1.5);
该图(仅用于说明我想要的内容)如下所示:
如果不使用matlab2tikz
,如何使用 pastebin 链接中提供的数据在 pgfplots 中直接重新创建相同的图?理想情况下,该方法应该能够自动获取任意数量的 y 数据并适当设置每条线的颜色。
谢谢
答案1
您可以使用\foreach
循环绘制所有列,并通过设置来更改颜色mesh, point meta=<looping variable>
:
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepackage{filecontents}
\begin{filecontents}{data.txt}
x y1 y2 y3 y4 y5 y6 y7 y8 y9 y10
0.001 0.9990005 0.947831109 0.899282645 0.853220861 0.809518389 0.768054383 0.728714188 0.691389021 0.65597567 0.622376212
0.053578947 0.998900605 0.94276631 0.889786543 0.839784031 0.792591464 0.748050933 0.706013406 0.666338223 0.62889263 0.593551332
0.106157895 0.99880072 0.937728576 0.880390717 0.826558809 0.776018478 0.72856846 0.684019795 0.64219508 0.602927757 0.566061454
0.158736842 0.998700845 0.93271776 0.871094107 0.813541862 0.759792031 0.709593394 0.662711326 0.618926706 0.578034888 0.539844748
0.211315789 0.99860098 0.927733721 0.861895666 0.800729911 0.743904877 0.69111252 0.642066654 0.596501404 0.554169761 0.51484225
0.263894737 0.998501124 0.922776314 0.852794357 0.788119727 0.728349922 0.673112969 0.622065102 0.574888628 0.531289946 0.490997723
0.316473684 0.998401279 0.917845397 0.843789155 0.775708133 0.713120219 0.655582203 0.602686635 0.554058939 0.509354762 0.468257537
0.369052632 0.998301444 0.912940828 0.834879045 0.763492001 0.698208966 0.638508014 0.583911842 0.533983962 0.488325209 0.446570545
0.421631579 0.998201619 0.908062468 0.826063022 0.751468253 0.683609507 0.62187851 0.565721919 0.514636354 0.468163895 0.42588797
0.474210526 0.998101804 0.903210176 0.817340093 0.739633859 0.66931532 0.605682111 0.548098645 0.495989759 0.448834974 0.406163293
0.526789474 0.998001999 0.898383812 0.808709275 0.727985838 0.655320022 0.589907536 0.531024368 0.478018778 0.43030408 0.38735215
0.579368421 0.997902203 0.893583238 0.800169596 0.716521254 0.641617365 0.574543798 0.514481986 0.460698932 0.412538264 0.36941223
0.631947368 0.997802418 0.888808316 0.791720092 0.705237218 0.628201228 0.559580199 0.49845493 0.444006628 0.39550594 0.352303183
0.684526316 0.997702643 0.884058909 0.783359812 0.694130888 0.615065621 0.545006316 0.482927146 0.427919129 0.379176823 0.33598653
0.737105263 0.997602878 0.879334882 0.775087814 0.683199464 0.602204678 0.530812001 0.467883081 0.412414521 0.363521881 0.320425569
0.789684211 0.997503122 0.874636097 0.766903165 0.672440192 0.589612656 0.516987366 0.453307666 0.397471685 0.348513279 0.305585303
0.842263158 0.997403377 0.86996242 0.758804943 0.661850361 0.577283931 0.503522784 0.439186301 0.383070267 0.334124332 0.291432352
0.894842105 0.997303642 0.865313718 0.750792236 0.651427302 0.565212998 0.490408879 0.425504844 0.369190649 0.320329456 0.277934885
0.947421053 0.997203916 0.860689856 0.742864139 0.64116839 0.553394467 0.477636516 0.412249588 0.355813925 0.307104125 0.265062542
1 0.997104201 0.856090703 0.735019761 0.631071038 0.541823059 0.4651968 0.399407259 0.342921874 0.294424823 0.252786372
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[colormap/jet]
\foreach \i in {1,...,10}{
\addplot [very thick, no markers, mesh, point meta=\i] table [y index=\i] {data.txt};
}
\end{axis}
\end{tikzpicture}
\end{document}