4

This is what I'm trying to draw: enter image description here

I was able to draw this graph (code below): enter image description here But I am not able to create another change above the first one, nor "labeling" the nodes with other nodes.

Any ideas?

What I have done so far:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains}
\makeatletter
\tikzset{reset join/.code={\def\tikz@after@path{}}}
\makeatother

\begin{document}
\begin{tikzpicture}
[
node distance = 3mm and 9mm,
start chain = A going right,
 dot/.style = {shape=circle, fill=black, inner sep=0mm, minimum size=1mm,
               node contents={}, join, on chain=A},           
rdot/.style = {dot, fill=red, minimum size=1.5mm},
bdot/.style = {dot, fill=blue, minimum size=1.5mm},
gdot/.style = {dot, fill=green, minimum size=1.5mm},
ydot/.style = {dot, fill=yellow, minimum size=1.5mm},
                    ]
\node[dot,label=below:0];             % A-1
\node[rdot,label=below:$\frac{1}{10}$];
\node[dot,coordinate];
\node[rdot,label=below:$\frac{3}{10}$];
\node[dot,coordinate];           % A-5
\node[ydot,label=below:$\frac{5}{10}$];
\node[dot,coordinate];  
\node[rdot,label=below:$\frac{7}{10}$];
\node[dot,coordinate];
\node[rdot,label=below:$\frac{9}{10}$];
\node[dot,label=below:1];  
\end{tikzpicture}
\end{document}
4

Here's an alternative version without the use of libraries, but with a \foreach statement and some \if statements.

Output

enter image description here

Code

\documentclass[tikz,margin=10pt]{standalone}

\tikzset{
    dot/.style = {circle, fill=black, inner sep=0mm, minimum size=1mm},           
    rdot/.style = {dot, fill=red, minimum size=1.5mm},
    bdot/.style = {dot, fill=blue, minimum size=1.5mm},
    gdot/.style = {dot, fill=green!70!black, minimum size=1.5mm},
    ydot/.style = {dot, fill=yellow, minimum size=1.5mm},
}

\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (10,0);
\foreach \x/\style in {0,1/gdot,3/bdot,5,7/gdot,9/ydot,10}{%
    \ifodd\x
    \ifnum\x=5
        \node[ydot, label=below:$\frac{\x}{10}$] at (\x,0) {};
    \else
        \node[rdot, label=below:$\frac{\x}{10}$, label={[\style, label distance=1pt]}] at (\x,0) {};
    \fi
    \else
        \pgfmathtruncatemacro\nums{\x/10}
        \node[dot, label=below:\nums] at (\x,0) {};
    \fi
}
\end{tikzpicture}
\end{document}

Alternative code without foreach

\documentclass[tikz,margin=10pt]{standalone}

\tikzset{
    dot/.style = {circle, fill=black, inner sep=0mm, minimum size=1mm},           
    rdot/.style = {dot, fill=red, minimum size=1.5mm},
    bdot/.style = {dot, fill=blue, minimum size=1.5mm},
    gdot/.style = {dot, fill=green!70!black, minimum size=1.5mm},
    ydot/.style = {dot, fill=yellow, minimum size=1.5mm},
}

\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (10,0);

\node[dot, label=below:0] at (0,0) {};

\node[rdot, label=below:$\frac{1}{10}$, label={[gdot, label distance=1pt]}] at (1,0) {};
\node[rdot, label=below:$\frac{3}{10}$, label={[bdot, label distance=1pt]}] at (3,0) {};
\node[ydot, label=below:$\frac{5}{10}$] at (5,0) {};
\node[rdot, label=below:$\frac{7}{10}$, label={[gdot, label distance=1pt]}] at (7,0) {};
\node[rdot, label=below:$\frac{9}{10}$, label={[ydot, label distance=1pt]}] at (9,0) {};

\node[dot, label=below:1] at (10,0) {};

\end{tikzpicture}
\end{document}
  • 1
    If you want a solution with chains, just let me know. I'll provide one. – Alenanno Jun 2 '16 at 15:40
  • your solution is great. I will appreciate if you make each node manually, since the use of foreach make things harder to understand, and also I'm interested in extending it to other drawings as well. Use less nodes if it is too grueling. Thanks! – omerbp Jun 2 '16 at 15:49
  • @omerbp If you don't understand something from the code above, you can ask here in the comments. I'll be happy to explain. If you don't have experience with \foreach statements, it might seem odd, but they are very straightforward. I mean, especially if you need to do this multiple times, doing each node is going to be tedious and also make your code long. I'll include a normal version though. – Alenanno Jun 2 '16 at 15:52
  • @omerbp Did you want the other version using chain or not? It wasn't clear to me. – Alenanno Jun 2 '16 at 15:56
  • Your alternative solution is great, Thanks again! – omerbp Jun 2 '16 at 16:00
5

Additional color circles you can add with

\begin{scope}[dot/.append style={reset join}]
\node[gdot, above=of A-2];
\node[bdot, above=of A-4];
\node[gdot, above=of A-8];
\node[ydot, above=of A-10];
\end{scope}

after main nodes:

enter image description here

Complete code:

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{chains}
\makeatletter
\tikzset{reset join/.code={\def\tikz@after@path{}}}
\makeatother

\begin{document}
\begin{tikzpicture}[
node distance = 1mm and 9mm,
start chain = A going right,
 dot/.style = {shape=circle, fill=black, inner sep=0mm, minimum size=1mm,
               node contents={}, join, on chain=A},
rdot/.style = {dot, fill=red, minimum size=1.5mm},
bdot/.style = {dot, fill=blue, minimum size=1.5mm},
gdot/.style = {dot, fill=green, minimum size=1.5mm},
ydot/.style = {dot, fill=yellow, minimum size=1.5mm},
                    ]
\node[dot,label=below:0];             % A-1
\node[rdot,label=below:$\frac{1}{10}$];
\node[dot,coordinate];
\node[rdot,label=below:$\frac{3}{10}$];
\node[dot,coordinate];           % A-5
\node[ydot,label=below:$\frac{5}{10}$];
\node[dot,coordinate];
\node[rdot,label=below:$\frac{7}{10}$];
\node[dot,coordinate];
\node[rdot,label=below:$\frac{9}{10}$];
\node[dot,label=below:1];
%
\begin{scope}[dot/.append style={reset join}]
\node[gdot, above=of A-2];
\node[bdot, above=of A-4];
\node[gdot, above=of A-8];
\node[ydot, above=of A-10];
\end{scope}
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.