我尝试在所有 4 个图上添加线性回归线,但却无法处理它。
\addplot table[y={create col/linear regression={y=Y}}]{
(1.0, 0.816286793)
(1.5, 0.81474159)
(2.0, 0.77101728)
(2.5, 0.733906267)
(3.0, 0.693063823)
(4.0, 0.660231965)
(5.0, 0.62408223)
(6.0, 0.627902353)
};
我尝试在每个图下方插入上述代码来生成线性回归线,但总是出现错误?
\documentclass[12pt]{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{subfig}
\numberwithin{figure}{section}
\usepackage{wrapfig}
\graphicspath{{F}}
\usepackage{array}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{spy}
\usepackage{pgfplotstable}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[x post scale=2, y post scale=1.5,xlabel=Injection Pressure (Bar) ,ylabel=Coefficient Discharge, xmin=0, xmax=7, ymin=0.5, ymax=1]
\addlegendentry{1/4 Lift}
\addplot [color=blue,mark=o, smooth] coordinates {
(1.0, 0.816286793)
(1.5, 0.81474159)
(2.0, 0.77101728)
(2.5, 0.733906267)
(3.0, 0.693063823)
(4.0, 0.660231965)
(5.0, 0.62408223)
(6.0, 0.627902353)
};
\addlegendentry{1/2 Lift}
\addplot [color=red,mark=triangle, smooth] coordinates {
(1.0, 0.825289956)
(1.5, 0.808615713)
(2.0, 0.77101728)
(2.5, 0.731375556)
(3.0, 0.662742281)
(4.0, 0.657230911)
(5.0, 0.657635038)
(6.0, 0.661594675)
};
\addlegendentry{3/4 Lift}
\addplot [color=green,mark=10-pointed star, smooth] coordinates {
(1.0, 0.861302609)
(1.5, 0.852722025)
(2.0, 0.81911744)
(2.5, 0.685822753)
(3.0, 0.675737228)
(4.0, 0.681239346)
(5.0, 0.657635038)
(6.0, 0.66281985)
};
\addlegendentry{Full Lift}
\addplot [color=yellow,mark=+, smooth] coordinates {
(1.0, 0.862803136)
(1.5, 0.863136015)
(2.0, 0.824776283)
(2.5, 0.682026686)
(3.0, 0.679202547)
(4.0, 0.676737764)
(5.0, 0.669714049)
(6.0, 0.669558314)
};
\end{axis}%
\end{tikzpicture}%
\caption{Cd vs Re for straight nozzle}
\end{figure}
\end{document}
答案1
您必须添加一行包含列名的内容并删除圆括号和逗号:
\addplot table[y={create col/linear regression={y=Y}}]{
X Y
1.0 0.816286793
1.5 0.81474159
2.0 0.77101728
2.5 0.733906267
3.0 0.693063823
4.0 0.660231965
5.0 0.62408223
6.0 0.627902353
};
因为x
所有图的坐标都相等,所以您可以声明一个包含所有图的值的表。
\documentclass[12pt]{article}
\usepackage{pgfplotstable}% loads pgfplots, tikz, graphicx
\pgfplotsset{compat=1.14}% <- added!
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
% one table for all plots
\pgfplotstableread{
X Y1 Y2 Y3 Y4
1.0 0.816286793 0.825289956 0.861302609 0.862803136
1.5 0.81474159 0.808615713 0.852722025 0.863136015
2.0 0.77101728 0.77101728 0.81911744 0.824776283
2.5 0.733906267 0.731375556 0.685822753 0.682026686
3.0 0.693063823 0.662742281 0.675737228 0.679202547
4.0 0.660231965 0.657230911 0.681239346 0.676737764
5.0 0.62408223 0.657635038 0.657635038 0.669714049
6.0 0.627902353 0.661594675 0.66281985 0.669558314
}\data
\begin{axis}[
x post scale=2, y post scale=1.5,
xlabel=Injection Pressure (Bar) ,
ylabel=Coefficient Discharge,
xmin=0, xmax=7,
ymin=0.5, ymax=1,
regline/.style={dashed,forget plot}
]
\addlegendentry{1/4 Lift}
\addplot [color=blue,mark=o, smooth] table [y=Y1] {\data};
\addplot [blue,regline] table [y={create col/linear regression={y=Y1}}] {\data};
\addlegendentry{1/2 Lift}
\addplot [color=red,mark=triangle, smooth] table [y=Y2] {\data};
\addplot [red,regline] table [y={create col/linear regression={y=Y2}}] {\data};
\addlegendentry{3/4 Lift}
\addplot [color=green,mark=10-pointed star, smooth] table [y=Y3] {\data};
\addplot [green,regline] table [y={create col/linear regression={y=Y3}}] {\data};
\addlegendentry{Full Lift}
\addplot [color=yellow,mark=+, smooth] table [y=Y4] {\data};
\addplot [yellow,regline] table [y={create col/linear regression={y=Y4}}] {\data};
\end{axis}%
\end{tikzpicture}%
\caption{Cd vs Re for straight nozzle}
\end{figure}
\end{document}