
我有一個資料集,其中行表示年份,列表示公司。每年,我都想繪製第一家公司相對於其他公司的排名。
在下面的範例中,2013 年並列第五(如果這太棘手,我可以打破舍入),因為2013 行中的單元格1 到8(例如單元格0)的排序為:0.67 (F3; 1st) > 0.54 ( F6;第 2)> 0.48(F2;第 3)> 0.46(F8;第 4)> 0.38(F1 和 F4;第 5 關節)。 2014年起,F1排名第六。
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotstableread[col sep=tab,row sep=\\,header=false]{
% year F1 F2 F3 F4 F5 F6 F7 F8
2013 0.38 0.48 0.67 0.38 0.34 0.54 0.32 0.46 \\
2014 0.37 0.48 0.70 0.39 0.34 0.54 0.31 0.50 \\
2015 0.35 0.44 0.68 0.38 0.30 0.53 0.28 0.49 \\
2016 0.33 0.43 0.67 0.35 0.29 0.51 0.29 0.47 \\
}\datatable
\begin{tikzpicture}
\begin{axis}[
ylabel = rank, y dir = reverse,
]
\addplot table [x expr=\coordindex, y expr= % what goes here?
] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}
我想要的結果的手工編碼版本如下。
答案1
那麼您正在尋找類似以下內容的內容嗎?希望這是不言自明的...
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{
compat=1.3,
/pgf/declare function={
MyRank(\a,\b,\c,\d,\e,\f,\g,\h) =
ifthenelse( \a == min(\a,\b,\c,\d,\e,\f,\g,\h), 1,
ifthenelse( \b == min(\a,\b,\c,\d,\e,\f,\g,\h), 2,
ifthenelse( \c == min(\a,\b,\c,\d,\e,\f,\g,\h), 3,
ifthenelse( \d == min(\a,\b,\c,\d,\e,\f,\g,\h), 4,
ifthenelse( \e == min(\a,\b,\c,\d,\e,\f,\g,\h), 5,
ifthenelse( \f == min(\a,\b,\c,\d,\e,\f,\g,\h), 6,
ifthenelse( \g == min(\a,\b,\c,\d,\e,\f,\g,\h), 7,
8)))))));
},
}
% (please note that I have changed the first value of F5)
\pgfplotstableread{
year F1 F2 F3 F4 F5 F6 F7 F8
2013 0.38 0.48 0.67 0.38 0.30 0.54 0.32 0.46
2014 0.37 0.48 0.70 0.39 0.34 0.54 0.31 0.50
2015 0.35 0.44 0.68 0.38 0.30 0.53 0.28 0.49
2016 0.33 0.43 0.67 0.35 0.29 0.51 0.29 0.47
}\datatable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ylabel=rank,
y dir=reverse,
xtick distance=1,
ytick distance=1,
]
\addplot table [
x expr=\coordindex,
y expr={
MyRank(
\thisrow{F1},
\thisrow{F2},
\thisrow{F3},
\thisrow{F4},
\thisrow{F5},
\thisrow{F6},
\thisrow{F7},
\thisrow{F8}
)
}
] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}