Search code examples
latextikz

ROC curve have negative values on the x-axis and greater than 100% on the y-axis


I am trying to draw a ROC curve with a smooth line (I need it smooth). But the line exceeds the X and Y-axis

how to fix the next latex instruction to get this correctly.

\begin{tikzpicture}
\begin{axis}[
    font=\sf,
    xlabel={False Positive Rate},
    ylabel={True Positive Rate},
    xmin= -0.05, xmax=1,
    ymin= 0, ymax=1.1,
    xtick={0,.2,.4,.6,.8,1},
    ytick={0,.2,.4,.6,.8,1},
    legend pos=south east,
    no markers,
    every axis plot/.append style={ultra thick}
]
\addlegendimage{empty legend}
\addlegendentry{\hspace{-.6cm}\textbf{AUC}}

\addplot[smooth ,red ] 
coordinates {(0,0)(0.00635842,0.99631106)(1,1)}; \addlegendentry{L =  \%99.49}
\end{axis}
\end{tikzpicture}

Solution

  • Your second point (0.00635842,0.99631106) has the y coordinate very close to one, hence when you try to draw a smooth plot the drawing algorithm fails to draw a graph that remains under the line y=1. Indeed, the pgf manual says

    Note that the smoothing algorithm is not very intelligent. You will get the best results if the bending angles are small, that is, less than about 30◦ and, even more importantly, if the distances between points are about the same all over the plotting path.

    In your case, the points do not satisfy these requirements, they define an almost right angle.

    You may have a little control by using the tension option:

    \documentclass{standalone}
    
    \usepackage{tikz}
    \usepackage{pgfplots}
    
    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        font=\sf,
        xlabel={False Positive Rate},
        ylabel={True Positive Rate},
        xmin= -0.05, xmax=1,
        ymin= 0, ymax=1.1,
        xtick={0,.2,.4,.6,.8,1},
        ytick={0,.2,.4,.6,.8,1},
        legend pos=south east,
        no markers,
        %every axis plot/.append style={ultra thick}
    ]
    \addlegendimage{empty legend}
    \addlegendentry{\hspace{-.6cm}\textbf{AUC}}
    
    \addplot[smooth,red, tension=0.03 ] 
    coordinates {(0,0)(0.00635842,0.99631106)(1,1)}; 
    \addlegendentry{L =  \%99.49}
    %% Just to check how much the red graph goes outside the correct region.
    \addplot[black,dashed] coordinates{(0,1) (1,1)};
    \addplot[black,dashed] coordinates{(0,0) (0,1)};
    \end{axis}
    \end{tikzpicture}