如何在图表上添加对角线。(红色虚线)
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\begin{axis}[xlabel={False Positive Rate}, ylabel={True Positive Rate},legend pos={north east}, scaled ticks = false,tick label style={/pgf/number format/fixed,/pgf/number format/precision=3},xmax=1,ymax=1,xmin=0,ymin=0]
\addplot[
scatter,only marks,scatter src=explicit symbolic,
scatter/classes={
a={mark=*,draw=bblue,fill=bblue},
b={mark=*,draw=rred,fill=rred}
}]
table[x=x,y=y,meta=label]{
x y label
0.316 0.888 a
0.342 0.902 b
};
\legend{AB,AA}
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
如果您要查找 y=x 那条线,则只需\addplot [red] {x};
。如果您要查找更通用的轴的左下角到轴的右上角,请使用rel axis cs
坐标系。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}
\begin{axis}[xlabel={False Positive Rate}, ylabel={True Positive Rate},legend pos={north east}, scaled ticks = false,tick label style={/pgf/number format/fixed,/pgf/number format/precision=3},xmax=1,ymax=1,xmin=0,ymin=0]
\addplot[
scatter,only marks,scatter src=explicit symbolic,
scatter/classes={
a={mark=*,draw=blue,fill=blue},
b={mark=*,draw=red,fill=red}
}]
table[x=x,y=y,meta=label]{
x y label
0.316 0.888 a
0.342 0.902 b
};
\addplot [red,samples at={0,1}] {x};
\draw [blue,dashed] (rel axis cs:0,0) -- (rel axis cs:1,1);
\legend{AB,AA}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}