我们可以轻松绘制以下数据:
\RequirePackage{filecontents}
\begin{filecontents}{data.dat}
X P $Q_B$ $Q_C$ $Q_D$
1 A1 2 6 7
2 A2 3 65 87
3 A3 1 45 65
4 A4 4 54 45
5 A5 6 56 34
6 A6 8 34 23
7 A7 9 45 45
8 A8 13 23 56
9 A9 4.6 6 87
10 A10 4 56 89
\end{filecontents}
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\pagestyle{empty}
\begin{document}
\pgfplotstabletypeset[columns/P/.style={string type}]{data.dat}
\vspace{1cm}
\begin{tikzpicture}
\begin{axis}[
xlabel=Q Series,
ylabel=P Values,
xticklabels from table={data.dat}{P},xtick=data]
\addplot[blue,thick,mark=square*] table [y=$Q_B$,x=X]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot[red,thick,mark=square*] table [y=$Q_C$,x=X]{data.dat};
\addlegendentry{$Q_C$ series}
\addplot[black,dashed,thick,mark=square*] table [y=$Q_D$,x=X]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}
\end{document}
问题:
有没有办法绘制如下所示的分数数据:
X P $Q_B$ $Q_C$ $Q_D$
1 A1 1/2 1/6 1/7
2 A2 1/3 65 87
3 A3 1 1/45 65
4 A4 4 54 45
5 A5 6 56 34
6 A6 8 1/4^3 23
7 A7 9 45 45
8 A8 13 23 56
9 A9 1/4.6 6 87
10 A10 4 56 89
答案1
您可以使用y expr
和相应的进一步语法来做到这一点。
\begin{filecontents*}{data.dat}
X P $Q_B$ $Q_C$ $Q_D$
1 A1 1/2 1/6 1/7
2 A2 1/3 65 87
3 A3 1 1/45 65
4 A4 4 54 45
5 A5 6 56 34
6 A6 8 1/4^3 23
7 A7 9 45 45
8 A8 13 23 56
9 A9 1/4.6 6 87
10 A10 4 56 89
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=Q Series,
ylabel=P Values,
xtick=data,
xticklabels from table={data.dat}{P},
]
\addplot [blue,thick,mark=square*] table [
x=X,
% % this worked so far, ...
% y=$Q_B$,
% ... but now we have to use this and it works
y expr=\thisrow{$Q_B$},
]{data.dat};
\addlegendentry{$Q_B$ series}
\addplot [red,thick,mark=square*] table [
x=X,
y expr=\thisrow{$Q_C$},
]{data.dat};
\addlegendentry{$Q_C$ series}
\addplot [black,dashed,thick,mark=square*] table [
x=X,
y expr=\thisrow{$Q_D$},
]{data.dat};
\addlegendentry{$Q_D$ series}
\end{axis}
\end{tikzpicture}
\end{document}