# How to draw this piecewise graph using PGF/TikZ?

Given

Is there an easy way to define the condition as "rational" and "irrational" using PGF/TikZ? A minimal example would be greatly appreciated.

This is one of the rational/irrational graphs in Calculus by Michael Spivak book, page 97.

• There is no way let alone easy one. Measure zero sets on finite precision machines. I'm not touching that... :) – percusse Oct 29 '12 at 23:00
• I'm sure PSTricks has a package for that =) – Jake Oct 29 '12 at 23:09
• Besides symbolically, I don't even know how one could represent an irrational number in any computer system since the precision is always fixed, not matter how many decimal digits you allow for. – Peter Grill Oct 29 '12 at 23:09
• There is no way to draw this with any package. Rational and irrational numbers are dense in the reals, so what you'd see are just two segments. – egreg Oct 29 '12 at 23:14
• A pixel is a small square, that covers infinitely many rational and irrational points. So there's no way to distinguish them with a picture. – egreg Oct 29 '12 at 23:27

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 2ε 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.

• What is the area under the curve from 0 to a positive x? – kiss my armpit Oct 30 '12 at 7:19
• @ガベージコレクタ The integral is of course x^2/2; so what? – egreg Oct 30 '12 at 7:27
• @ガベージコレクタ: egreg, I thought the integral required continuity on some closed interval? – Peter Grill Oct 30 '12 at 7:37
• @egreg: For me, it looks like a paradox because of the infinite number of discontinuities. – kiss my armpit Oct 30 '12 at 8:45
• @PeterGrill It depends what integral you're referring to. But that function is almost everywhere equal to g(x)=x, so their Lebesgue integrals are the same. It's not Riemann integrable, I believe. – egreg Oct 30 '12 at 8:47

## 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

• I think that this is the nicest representation and the nicest answer. – Loop Space Oct 30 '12 at 9:22
\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:

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}

• I believe Spivak must have a reason to plot the graph like that. For me, it looks like he tried to draw the difference between rational and irrationals. In fact, there are far more irrationals than rationals. – Chan Oct 30 '12 at 4:44
• Even between any two rational numbers there are an infinite number of other rationals to begin with ... – Qrrbrbirlbel Oct 30 '12 at 4:52
• @Qrrbrbirlbel: Yes, that case is already covered in my statements 1 and 2 above. :-) – Peter Grill Oct 30 '12 at 7:08
• @Chan Probably Spivak had a reason for making an obviously wrong diagram; I can't see one. – egreg Oct 30 '12 at 7:26

Here's an option using pgfplots:

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
only marks,
clip=false,
mark=*,
mark options={mark size=3pt,color=brown},
xtick=\empty,
ytick=\empty,
enlargelimits=0.2
]
\node at (axis cs:6.2,1.45) {$f(x)=\begin{cases}x,\quad&\text{$x$rational.} \\ 0,\quad&\text{$x$irrational.}\end{cases}$};
\end{axis}
\end{tikzpicture}

\end{document}


• From this graph, it is difficult (at least for me) to determine whether (0,0) belongs to the first case. :-) – kiss my armpit Oct 30 '12 at 9:21

It is the highest resolution I can achieve for you!

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\usepackage{amsmath}
\begin{document}
\begin{pspicture}(-3.5,-3.5)(3.5,3.5)
\psaxes[linecolor=lightgray]{->}(0,0)(-3.5,-3.5)(3.5,3.5)[$x$,-90][$y$,180]
\psplot[linecolor=red]{-3}{3}{0}% for irrational
\psplot[linecolor=blue]{-3}{3}{x}% for rational
\rput(1.75,-1.5){\scriptsize
$f(x)=\begin{cases} \color{blue}x,&\text{\color{blue}$x$rational.}\\ \color{red}0,&\text{\color{red}$x$irrational.} \end{cases}$}
\end{pspicture}
\end{document}

• \begin {irony} Oh, I already had the hope, that pstricks would be sophisticated enough to provide a proper solution. Sad. just kidding \end{irony} – Benedikt Bauer Oct 30 '12 at 7:00
• @BenediktBauer: Don't be sad. It is just the beginning of your journey with PSTricks. The summary is that PSTricks is superior to TikZ from any point of views. :-) – kiss my armpit Oct 30 '12 at 7:11
• @ガベージコレクタ: Hilarious :-) – Peter Grill Oct 30 '12 at 7:33
• I'm disappointed. I was expecting an animation with the rational points appearing at rational times. – Loop Space Oct 30 '12 at 9:23
• Clearly, choosing to animate this graph would be an irrational decision and therefore the animated graph should consist only of the values on the x-axis. – Loop Space Oct 30 '12 at 9:30