14

I can't seem to figure out why, but TikZ isn't properly plotting the floor function. Here is my code:

\begin{tikzpicture}[xscale=1,yscale=1]
\draw[step=.5cm,gray,very thin] (0,0) grid (8,8);
\draw[red, thick, domain=0:8] plot (\x, {floor(\x)}) node[right] {$\lfloor x \rfloor$};
\draw[black,  thick, domain=0:8] plot(\x, \x) node[right] {$x$};
\draw[black, thick, domain=0:8] plot(\x, \x/2) node[right] {$x/2$};
\draw[blue, thick,domain=0:8] plot(\x, {floor((\x/2))}) node[right] {$\lfloor x/2 \rfloor$};
\draw [<->] (0,8) -- (0,0) -- (8,0);
\end{tikzpicture}

I am getting the following result (note the diagonal lines between the flat segments, which should not be present):

3
  • It looks to me as though TiKZ is sampling at data points which are unevenly spaced from grid cell to grid cell. I suspect that the plot is perfectly correct, except that the points on the x-axis which it is sampling at is much more coarse than you might like. Commented Jun 16, 2012 at 23:19
  • Ah, I see. Is there a way to make it sample a more dense set of points?
    – mlbaker
    Commented Jun 16, 2012 at 23:21
  • Related: How to Graph Floor/Ceiling Functions in LaTeX (PGFPlots)
    – Jake
    Commented Feb 12, 2014 at 8:00

3 Answers 3

13

As suggested in a comment, the points sampled by tikz are spaced too far apart. You can coerce tikz to sample more densely, by writing:

\begin{tikzpicture}[xscale=1,yscale=1]
\draw[step=.5cm,gray,very thin] (0,0) grid (8,8);
\draw[red, thick, domain=0:8, samples=300] plot (\x, {floor(\x)}) node[right] {$\lfloor x \rfloor$};
\draw[black,  thick, domain=0:8] plot(\x, \x) node[right] {$x$};
\draw[black, thick, domain=0:8] plot(\x, \x/2) node[right] {$x/2$};
\draw[blue, thick,domain=0:8, samples=300] plot(\x, {floor((\x/2))}) node[right] {$\lfloor x/2 \rfloor$};
\draw [<->] (0,8) -- (0,0) -- (8,0);
\end{tikzpicture}

Note the option samples which set the number of samples to be evaluated on the given domain.

enter image description here

EDIT: Another way, which helps you avoid setting a large number of sampling points (in your case that might be better, since you have few 'interesting' points on your plot) is to specify the actual points, where the function should be evaluated:

\draw[blue, thick, samples at={0,1.999,2,3.999,4,5.999,6,7.999}] plot(\x, {floor((\x/2))}) node[right] {$\lfloor x/2 \rfloor$};

Just be careful to give all relevant points, otherwise tikz will draw a straight line regardless whether the function looks that way or not between the two points. (You can check that by erasing a value from the list between the braces.)

EDIT2: As stated in the comments, the artifacts (i.e. the sloped lines) can be eliminated by writing for the red line:

\draw[red, thick, domain=0:8, samples at={0,...,7,7.999},const plot] plot (\x, {floor(\x)}) node[right] {$\lfloor x \rfloor$};

and for the blue line:

\draw[blue, thick, samples at={0,2,...,6,7.999},const plot] plot(\x, {floor((\x/2))}) node[right] {$\lfloor x/2 \rfloor$};

The difference is that in the case of the blue line only the points with even abscissa are evaluated, since the function only jumps there.

3
  • 2
    Another option would be to use samples at={0,...,7,7.999}] plot [const plot], where const plot connects the segments using proper orthogonal lines, which will avoid the artifacts that are sometimes visible on the screen using other methods.
    – Jake
    Commented Jun 17, 2012 at 6:23
  • 1
    Like Jake wrotes samples at={0,...,7,7.999},const plot] for the red and the blue curves and samples=2 for the straight lines. Commented Jun 17, 2012 at 7:18
  • Thanks, I edited my answer. It's great to learn some new tricks. :)
    – Count Zero
    Commented Jun 17, 2012 at 9:56
6

I think, both solutions are wrong or lets say not fine. You should plot something like that:

\foreach \a in {1,2,...,7}
\draw[very thick, blue] plot[domain=\a:\a+1] (\x,{floor(\x)}) node[right] {\footnotesize $[x]$};
3
  • +1 You're absolutely right: the floor function is discontinuous. Commented Feb 12, 2014 at 6:14
  • 4
    There is also a const plot handler which has the same effect for plots. Commented Feb 12, 2014 at 7:36
  • In the context of multi-valued functions yes but for single valued functions the plot is unambigious so it's a matter of interpretation.
    – percusse
    Commented Feb 12, 2014 at 12:19
2

this is my version

\begin{tikzpicture}
    \tkzInit[xmin = -5, xmax = 5, ymin = -5, ymax = 5]
    \tkzAxeXY
    \foreach \a in {-5,...,5}{
        \draw[blue] (\a, \a) -- (\a + 1, \a);
        \node [circle, draw, fill=none, line width = .5pt, color = blue, inner sep = 0pt, minimum size = 3pt] (ca) at (\a, \a) {};
        \node [circle, draw, fill, line width = .5pt, color = blue, inner sep = 0pt, minimum size = 3pt] (ca) at (\a + 1, \a) {};
    }
\end{tikzpicture}

enter image description here

1
  • Welcome to TeX.SX! Please don't post a code fragment, but always a complete minimal working example.
    – CarLaTeX
    Commented Aug 3, 2017 at 4:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.