A real function on a compact set is continuous if and only if its graph is compact I have a problem with the following
Let $X$ be a compact subset of $\mathbb{R}$ and let $f$ be a real-valued function on $X$.  Prove that $f$ is continuous if and only if $\{(x,f(x)) \mid x\in X\}$ is compact subset of ${\mathbb R}^2$.
I have done $\Rightarrow$ direction but with the other direction I could use some help. 
 A: A general topology answer: suppose $f: X \rightarrow Y$ has a compact graph $\Gamma(f) = \left\{(x,f(x)): x \in X\right\} \subset X \times Y$. Also suppose $X$ is Hausdorff.
Now let $F \subset Y$ be closed. To show continuity of $f$, it suffices to show that $f^{-1}[F]$ is closed in $X$. Define $\pi: X \times Y \rightarrow X$ by $\pi(x,y) = x$, the projection onto the first coordinate, which is continuous. Then:
$$f^{-1}[F] = \pi[(X \times F) \cap \Gamma(f)]$$
($x \in f^{-1}[F]$ iff $f(x) \in F$, which means $(x,f(x)) \in \Gamma(f) \cap (X \times F)$, and $\pi(x,f(x)) = x$, so $x$ is in the right hand side. On the other hand, if $x$ is in the right hand side, we have $(x,y) \in (X \times F) \cap \Gamma(f)$ for some $y (\in F)$ and being in $\Gamma(f)$ forces $y = f(x)$ , and so $y = f(x) \in F$, so $x \in f^{-1}[F]$.)
Now $X \times F$ is closed in $X \times Y$ so its intersection with $\Gamma(f)$ is closed in $\Gamma(f)$, which is compact. So $(X \times F) \cap \Gamma(f)$ is compact as well, being closed in a compact set. And so its continuous image under $\pi$, which equals $f^{-1}[F]$ as we saw, is compact too. But if $X$ is Hausdorff, this implies it's closed, and we are done.
So the only thing we need of being a subset of $\mathbb{R}$ is that it guarantees Hausdorffness.
A: For the $\,"\Longleftarrow"\,$ direction we use proof by contradiction.
If not, let $x_n\to x$, while $f(x_n)\not\to f(x)$. This implies that there exists an $\varepsilon>0$ and a subsequence $\{f(x_{k_n})\}_{n\in\mathbb N}$ with the property
$$
\lvert \,f(x_{k_n})-f(x)\rvert\ge\varepsilon.
$$
But as the graph $\mathscr G(f)$ of $f$ is compact, so is its $y-$projection, and hence $\{f(x_{k_n})\}_{n\in\mathbb N}$ possesses a convergent subsequence, call it $\{f(y_n)\}_{n\in\mathbb N}$, and the limit is $z\ne f(x)$. So we have
$$
\big(y_n,f(y_n)\big)\to (x,z)\in\mathscr G(f).
$$
But this is a contradiction, as $f$ is a function, and
$$
\big(x,f(x)\big),\, (x,z)\in\mathscr G(f).
$$
