如何在 tikz 图片中的点前画一条线

如何在 tikz 图片中的点前画一条线

我在 tikz 图中绘制线条时遇到了问题。感觉解决方案应该非常简单,但我不知道如何解决它。当我绘制数据点和一条线时,线位于数据点后面,即使线的代码位于点的代码之后,而我需要线位于点之前。这不是我自己写的,而是使用 matlab2tikz 函数来生成代码(如果这相关的话)。

它的外观如下图所示: 在此处输入图片描述

以下是最小工作示例:

\documentclass{article}
\usepackage{pgfplots} 
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}

\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}
\begin{tikzpicture}

\begin{axis}[%
width=3.633in,
height=2.11in,
at={(0.609in,0.492in)},
scale only axis,
xmin=30.000,
xmax=80.000,
xlabel style={font=\fontsize{12pt}{1em}\color{white!15!black}\selectfont},
xlabel={Measurement},
ymin=30.000,
ymax=80.000,
ylabel style={font=\fontsize{12pt}{1em}\color{white!15!black}\selectfont},
ylabel={Prediction},
axis background/.style={fill=white}
]
\addplot [color=mycolor1, line width=1.0pt, only marks, mark size=3.5pt, mark=*, mark options={solid,             fill=blue, draw=black}, forget plot]
  table[row sep=crcr]{%
72.000  72.047\\
72.000  71.995\\
68.000  68.002\\
69.000  70.999\\
68.000  67.503\\
65.000  65.175\\
73.000  72.323\\
73.000  71.995\\
70.000  65.484\\
54.000  57.819\\
38.000  37.534\\
};
\addplot [color=red, line width=1.0pt, forget plot]
  table[row sep=crcr]{%
30.000  30.000\\
80.000  80.000\\
};
\node[right, align=left, font=\fontsize{12pt}{1em}\selectfont]
at (axis cs:60,41) {$\overline{R^{2}}=0.949$};
\node[right, align=left, font=\fontsize{12pt}{1em}\selectfont]
at (axis cs:55.1,36) {$RMSE = 2.484$};
\end{axis}
\end{tikzpicture}%

\end{document}

答案1

您可以在不同的图层上绘图并按照本文的说明排列它们:PGFPlots:了解如何在不同的图层上绘制图表并排列它们

另一种方法是在不同的环境中绘制线条axis,如下所示:

\documentclass{article}
\usepackage{pgfplots} 
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}

\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}
\begin{tikzpicture}

\begin{axis}[%
width=3.633in, height=2.11in,
at={(0.609in,0.492in)},
scale only axis,
xmin=30.000, xmax=80.000,
xlabel style={font=\fontsize{12pt}{1em}\color{white!15!black}\selectfont},
xlabel={Measurement},
ymin=30.000, ymax=80.000,
ylabel style={font=\fontsize{12pt}{1em}\color{white!15!black}\selectfont},
ylabel={Prediction},
axis background/.style={fill=white}
]
\addplot [color=mycolor1, line width=1.0pt, only marks, mark size=3.5pt, mark=*, mark options={solid,             fill=blue, draw=black}, forget plot]
  table[row sep=crcr]{%
72.000  72.047\\
72.000  71.995\\
68.000  68.002\\
69.000  70.999\\
68.000  67.503\\
65.000  65.175\\
73.000  72.323\\
73.000  71.995\\
70.000  65.484\\
54.000  57.819\\
38.000  37.534\\
};
\addplot [color=red, line width=1.0pt, forget plot]
  table[row sep=crcr]{%
30.000  30.000\\
80.000  80.000\\
};
\node[right, align=left, font=\fontsize{12pt}{1em}\selectfont]
at (axis cs:60,41) {$\overline{R^{2}}=0.949$};
\node[right, align=left, font=\fontsize{12pt}{1em}\selectfont]
at (axis cs:55.1,36) {$RMSE = 2.484$};
\end{axis}

\begin{axis}[
width=3.633in,height=2.11in,
at={(0.609in,0.492in)},
scale only axis,
xmin=30.000,xmax=80.000,
ymin=30.000,ymax=80.000,
ticks=none
]
\addplot [color=red, line width=1.0pt, forget plot]
  table[row sep=crcr]{%
30.000  30.000\\
80.000  80.000\\
};
\end{axis}
\end{tikzpicture}%

\end{document}

在此处输入图片描述

相关内容