5

How can one draw orthogonal tick marks on a function? For instance in the following plot I would like to make orthogonal tick marks on the function y=x^2, say.

\documentclass{article}
\usepackage{tikz} 
\begin{document}
\begin{tikzpicture}
      \draw[->] (-3,0) -- (4.2,0) node[right] {$x$};
      \draw[->] (0,-3) -- (0,4.2) node[above] {$y$};
      \draw[scale=0.5,domain=-3:3,smooth,variable=\x,blue] plot ({\x},{\x*\x});
      \draw[scale=0.5,domain=-3:3,smooth,variable=\y,red]  plot ({\y*\y},{\y});
    \end{tikzpicture}
\end{document}

I kind of know it will involve \foreach, but I am not sure how to proceed.

To make things clear, I would like to have the tick marks "following" the graph of the function. Also if there are two or more functions on the same plot, then each should have a separate option for decoration. Please see comments below.

  • What do you mean by "orthogonal tikz"? – Jake Nov 13 '13 at 12:21
  • Lines/arrows that are at 90 degrees angle to the graph. Of course a more general degree angle is better, but 90 degrees is OK too. – Your Majesty Nov 13 '13 at 12:22
  • 1
  • 1
    @LoveLearning: if the answer Jake linked helped you we might consider to close this question as a duplicate. Do you agree? – Claudio Fiandrino Nov 13 '13 at 12:51
  • I expected a better answer of the type: Let y be a function of x, i.e. y=f(x) then; \foreach x on the graph of "f" draw a vector at angle \theta with spacing S between the vectors. – Your Majesty Nov 13 '13 at 13:20
6

You can use a markings decoration for this.

Note that, for some reason, this doesn't work with the smooth option.

\documentclass{article}
\usepackage{tikz} 
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}[
    decoration={
        markings,
        mark={
            between positions 0 and 1 step 0.4cm
            with {\draw (0,-1mm) -- (0,1mm);}
        }}
    ]
      \draw[->] (-3,0) -- (4.2,0) node[right] {$x$};
      \draw[->] (0,-3) -- (0,4.2) node[above] {$y$};
      \draw[scale=0.5,domain=-3:3,blue, postaction=decorate] plot ({\x},{\x*\x});
      \draw[scale=0.5,domain=-3:3,variable=\y,red, postaction=decorate]  plot ({\y*\y},{\y});
    \end{tikzpicture}
\end{document}
  • Such an elegant solutions Jake, thank you very much! – Your Majesty Nov 13 '13 at 13:56
  • Do you think it is possible with two different styles/types of decoration (one for each line, say) in the same graph? – Your Majesty Nov 13 '13 at 13:59
  • 1
    Yes, that's possible. What styles were you thinking of? – Jake Nov 13 '13 at 14:05
  • 1
    Some thing like \draw[scale=0.5, domain=-3:3, blue, decoration={ markings, mark={ between positions 0 and 1 step 0.4cm with {\draw (0,-1mm) -- (0,1mm);} }}, postaction=decorate] plot ({\x},{\x*\x}); and change what you want. – user11232 Nov 14 '13 at 1:41
  • 1
    @LoveLearning: That will be a duplicate. \draw[scale=0.5, domain=-3:3, variable=\x,blue, decoration={ markings, mark={ between positions 0 and 1 step 0.4cm with {\draw (0,0) -- (0,1mm);} }}, postaction=decorate] plot ({\x},{\x*\x}); and \draw[scale=0.5, domain=-3:3, variable=\y,red, decoration={ markings, mark={ between positions 0 and 1 step 0.4cm with {\draw (0,0) -- (0,-1mm);} }}, postaction=decorate] plot ({\y*\y},{\y}); should do what you want. Remove the options for tikzpicture of course. – user11232 Nov 14 '13 at 13:11

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.