我想使用 pgfplots 绘制多条线。
每条线都应该有一种颜色,取决于它所属的组。
在所附的示例中,提供了一个表格,其中包含所有线条的坐标以及用于决定使用哪种颜色的附加信息
我尝试了一些方法,但显然我对 LATEX/pgfplots 不太熟悉。
如果有人能给我一点提示就太好了,
谢谢,托比
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\pgfplotstableread{
% This table holds the coordinates for multiple lines sharing the same x-values.
% The x-values are provided in the first column, the y-values in the subsequent columns.
% The character in the first row indicates the group the line belongs to
t a a a b b c c c c
-1 0 -2 -1 1 0 2 2 1 1
0 0 0 0 0 0 0 0 0 0
1 3 2 4 9 8 6 7 9 5
2 4.5 3 6 13 12 9 10 12 8
3 5.8 3.5 7 15 14 11 12 13 9
4 5.1 3.3 6.5 14 13 10 11 12 8.5
5 5 3.1 6.4 14 13 10 11 12 8.4
}\atable
% These are the colors I want to use for the 3 groups (a,b,c)
\definecolor{mycolorA}{named}{red}
\definecolor{mycolorB}{named}{green}
\definecolor{mycolorC}{named}{blue}
% Ok, let's plot the data
\begin{tikzpicture}
\begin{axis}[no markers]
% Loop over columns present (If someone knows a shorter way to do this please let me know)
\pgfplotstablegetcolsof{\atable}
\pgfmathparse{\pgfplotsretval-1}
\foreach \i in {1,...,\pgfmathresult}
{% The loop starts here
% Here somehow the character in the first row has to be read out.
% if the character in the first row of the current column \i
% is equal to 'a' then use mycolorA, if 'b' use mycolorB etc
\addplot table[x index=0,y index=\i] {\atable};
}
\end{axis};
\end{tikzpicture}
\end{document}
答案1
您还可以通过\pgfplotstableforeachcolumn
循环对每列执行操作,其中您将列名和索引作为宏获取,这样可以避免一些代码。我使用过xstring
Jake 提到的包,但我仍然认为这不是跟踪样式的好结构。我建议使用自定义循环列表。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{xstring}
\begin{document}
\pgfplotstableread{
t a a a b b c c c c
-1 0 -2 -1 1 0 2 2 1 1
0 0 0 0 0 0 0 0 0 0
1 3 2 4 9 8 6 7 9 5
2 4.5 3 6 13 12 9 10 12 8
3 5.8 3.5 7 15 14 11 12 13 9
4 5.1 3.3 6.5 14 13 10 11 12 8.5
5 5 3.1 6.4 14 13 10 11 12 8.4
}\atable
\begin{tikzpicture}
\begin{axis}[no markers,a/.style={red},b/.style={green},c/.style={blue}]
\pgfplotstableforeachcolumn\atable\as\colname{%
\StrLeft{\colname}{1}[\mycolname]
\ifnum\pgfplotstablecol=0\else%
\expandafter\addplot\expandafter[\mycolname] table[x=t,y index=\pgfplotstablecol] {\atable};
\fi
}
\end{axis};
\end{tikzpicture}
\end{document}
答案2
您可以使用 检索特定的列名\pgfplotstablegetcolumnnamebyindex
。在这种情况下,由于列名不是唯一的,因此此宏不仅会返回a
、b
和c
,还会返回a-index1
、a-index2
等。要从宏中提取第一个字符,您可以使用包xstring
(请参阅提取宏参数的第一个和最后一个字符?)。要使用宏调用样式,您可以使用 Andrew Stacey 的方法如何使用 pgfkeys 提交一组 tikz 命令?。请注意,您必须使用\pgfplotsinvokeforeach
而不是 来\foreach
确保宏在适当的时间执行:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{xstring}
\begin{document}
\pgfplotstableread{
% This table holds the coordinates for multiple lines sharing the same x-values.
% The x-values are provided in the first column, the y-values in the subsequent columns.
% The character in the first row indicates the group the line belongs to
t a a a b b c c c c
-1 0 -2 -1 1 0 2 2 1 1
0 0 0 0 0 0 0 0 0 0
1 3 2 4 9 8 6 7 9 5
2 4.5 3 6 13 12 9 10 12 8
3 5.8 3.5 7 15 14 11 12 13 9
4 5.1 3.3 6.5 14 13 10 11 12 8.5
5 5 3.1 6.4 14 13 10 11 12 8.4
}\atable
% These are the colors I want to use for the 3 groups (a,b,c)
\definecolor{mycolorA}{named}{red}
\definecolor{mycolorB}{named}{green}
\definecolor{mycolorC}{named}{blue}
\tikzset{
a/.style={
mycolorA
},
b/.style={
mycolorB
},
c/.style={
mycolorC
},
execute style/.style = {#1},
execute macro/.style = {execute style/.expand once=#1},
extract group/.code={
\pgfplotstablegetcolumnnamebyindex#1\of\atable\to\group
\StrLeft{\group}{1}[\firstletter]
}
}
% Ok, let's plot the data
\begin{tikzpicture}
\begin{axis}[no markers]
% Loop over columns present (If someone knows a shorter way to do this please let me know)
\pgfplotstablegetcolsof{\atable}
\pgfmathparse{\pgfplotsretval-1}
\pgfplotsinvokeforeach{1,...,\pgfmathresult}
{% The loop starts here
\addplot [extract group=#1, execute macro=\firstletter] table [x index=0,y index=#1] {\atable};
}
\end{axis};
\end{tikzpicture}
\end{document}