
我創建了以下程式碼,它似乎可以實現我想要的功能。即在表中新增一個附加行,\IDA
該行是四個值的平均值。然後,該行可用於 中的繪圖tikzpicture
。
\documentclass[tikz]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}
\begin{filecontents}{FileA.dat}
step ts1 ts2
1 3 4
2 7 6
3 4 6
4 5 6
5 5 7
6 9 3
\end{filecontents}
\begin{filecontents}{FileB.dat}
step tHIws
1 3
2 7
3 4
4 5
5 5
6 9
\end{filecontents}
% for calculations
\pgfplotstableread{FileB.dat}\SST
\pgfplotstableread{FileA.dat}\IDA
\pgfplotstableset{
create on use/tHIws/.style={
create col/expr={\thisrow{ts1}+\thisrow{ts2})/2}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=left,
tick align=outside,
xlabel=\texttt{step},
ylabel={$\theta_{a}$},
legend pos= north west,
legend columns=6,
legend style={fill=none, draw=none},
cycle multi list={
color list%
},
ymajorgrids,
]
\addplot table
[x=step, y=tHIws]
{\SST};
\addlegendentry{SSTe}
\addplot +[mark=*] table
[x=step, y=tHIws]
{\IDA};
\addlegendentry{IDA}
\addplot table
[x=step, y expr=(\thisrow{ts1}+\thisrow{ts2})/2]
{FileA.dat};
\addlegendentry{IDA}
\end{axis}
\end{tikzpicture}
\end{document}
然而,由於我需要對許多表格/數字執行此操作,因此我想以某種方式將這兩個命令合併為一個,或者至少確保上述命令\pgfplotstableset
僅適用於表格\IDA
。
有沒有辦法做到這一點?
由於打擊樂的回答而編輯。我實際上嘗試過將其轉移到 tikxpictures 但到目前為止失敗了。
編輯2:完整範例!
編輯 3:現在有內部數據。但是,我在編譯時遇到錯誤:“無法從表格‘FileA.dat’中檢索列‘step’。”但我似乎無法發現錯誤 - 我想我是“Betriebsblind”
編輯4:為內聯文件內容添加了缺少的usepackage
答案1
關於編輯,更容易將表達式放入樣式中並在需要時套用它。這裡我稱之為mymedian
\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}
\pgfplotstableread{
step ts1 ts2 ts3 ts4
1 3 4 11 14
2 7 6 22 26
3 4 6 34 31
4 5 6 45 46
5 5 7 55 57
6 9 3 69 63
}\SST
% for calculations
\pgfplotsset{mymedian/.style={
x=step, y expr=(\thisrow{ts1}+\thisrow{ts2}+\thisrow{ts3}+\thisrow{ts4})/4
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=left,
tick align=outside,
xlabel=\texttt{step},
ylabel={$\theta_{a}$},
legend pos= north west,
legend columns=6,
legend style={fill=none, draw=none},
cycle multi list={
color list%
},
ymajorgrids,
]
\addplot table[mymedian] {\SST};
\addlegendentry{SSTmean}
\addplot table[x=step,y=ts3] {\SST};
\addlegendentry{SSTS3}
\end{axis}
\end{tikzpicture}
\end{document}