How to draw this piecewise graph using PGF/TikZ?

There is no way to represent graphically this function. Your drawing tool has a thickness. If you try representing the point (1/2,0) that belongs to the graph of the Dirichlet function and is the thickness of the pencil, you'll be covering infinitely many points of the form (t,0), with t irrational that don't belong to the graph: there are infinitely many irrational numbers in the interval (-ε+1/2,ε+1/2), for any ε>0. The same if you want to draw a point of the graph with irrational x-coordinate.

Apart from this, for this kind of drawings you need numbers in floating point representation, which are all rational; but not even all rational numbers in the interval [0,1] are representable in the computer as floating point numbers.

Thus the best representation of this function you'd get would be two segments, which is useless.


Code

\documentclass[tikz,border=2pt]{standalone}
\usepackage{mathtools}
\begin{document}
\begin{tikzpicture}
\draw (-2,0) -- (4,0);
\draw (0,-2) -- (0,4);

\foreach \x in {-2.0, -1.9, ..., 3.01}
    \fill (\x,0) circle (1pt);
\foreach \x in {-1.95, -1.85, ..., 3.01}
    \fill (\x,\x) circle (1pt);

    \node at (4,1) {
    $f(x) = \begin{cases*}
        x, & $x$ rational \\
        0, & $x$ irrational \\
    \end{cases*}
    $};
\end{tikzpicture}
\end{document}

Output

Output


\begin{rant}  

I am truly shocked that any Calculus textbook would show such a graph for that function. I am going to add that to my book that I have been working on entitled "Obviously Wrong! " BTW, this is the first ever public announcement of this upcoming book... It was never intended to be technical book, but this one MUST go in there. Although, I do have one more really shocking mathematical example, but will save that for the upcoming interview.

\end{rant}

Since others have given their graphs, here is mine, and the rational behind it./ Given that

  1. Between any two rational numbers there are an infinite number of irrationals, and
  2. Between any two irrational numbers there are an infinite number of rational numbers

I would argue that a better representation of the given function is:

enter image description here

If you are concerned that you do not see the discontinuities, that just means that you have not zoomed in enough. :-) Keep zooming in and you will see the discontinuities.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  clip=false,
  mark=none,
  ymin=-4.5, ymax=4.5,
  xmin=-4.5, xmax=4.5
]
    \addplot [red,  ultra thick, domain=-4:4, latex-latex] {0};
    \addplot [blue, ultra thick, domain=-4:4, latex-latex] {x};
    \node at (axis cs:5.5,1.3) 
        {$f(x)=
            \begin{cases}
                \textcolor{blue}{x},\quad&\text{\textcolor{blue}{$x$ rational}} \\ 
                \textcolor{red}{0},\quad&\text{\textcolor{red}{$x$ irrational}}
            \end{cases}
        $};
\end{axis}
\end{tikzpicture}

\end{document}