Wrong figure reference number

When compiling your MWE with pdflatex I get the error

! Package caption Error: Continued `figure' after `??'.

Whatever that means … (I have used neither caption nor subcaption before.)

Apparently caption is confused when you use \ContinuedFloat in a figure environment when the preceding one doesn’t had a \caption; which isn’t very surprising because there isn’t much to continue. To fix this, add a \phantomcaption to the first figure environment[See Axel Sommerfeldt's comment.]

If you do want to include a caption in the first figure environment, of course, you must not use \phantomcaption.

Code

Note that I have removed every reference to an external graphic because

  • I don’t have them.
  • I don’t need them.

I also removed \usepackage{caption}, it does get already loaded by subcaption.
The chngcntr package does not interfere with this MWE.


\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{chngcntr}
\begin{document}
\begin{figure}
  \caption{fig:before}\label{fig:before}
\end{figure}
Figure~\ref{fig:before}.

I'm trying to reference Figure~\ref{fig:regress}, subfigure~\ref{fig:linfit} and \ref{fig:quadfit} as well as \ref{fig:cubicfit} here.

\hrulefill
\begin{figure}[htp]
  \centering
  \begin{subfigure}{0.8\textwidth}
    \caption{Scatter plot fitted with straight line}
    \label{fig:linfit}
  \end{subfigure}%

  \begin{subfigure}{0.8\textwidth}
    \caption{Scatter plot fitted with 2nd degree polynomial}
    \label{fig:quadfit}
  \end{subfigure}
  \phantomcaption%                                                                                                                  either a phantom caption
%  \caption{Scatteru plots of wavelength vs. pixel position for the spectrum from night 1, fitted with polynomials of orders 1-3.}% or a real one
\end{figure}

\hrulefill
\begin{figure}[htp]
  \ContinuedFloat
  \centering
  \begin{subfigure}{0.8\textwidth}
    \caption{Scatter plot fitted with 3rd degree polynomial} 
    \label{fig:cubicfit}
  \end{subfigure}
  \caption{Scatteru plots of wavelength vs. pixel position for the spectrum from night 1, fitted with polynomials of orders 1-3.}   \label{fig:regress}
\end{figure}
\end{document}

Output

enter image description here