1

I am looking to automate (simplify) the path drawn in blue on my MWE. I am trying to avoid nodes if possible as this is nested in a tikzpicture.

Ideally I was hoping to simply specify the begin and end coordinates and adjust the step so to speak and perhaps amplitude. Perhaps using pgfplots

This is the result of my MWE: (crappy screenshot)

Sample

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{snakes,backgrounds,arrows,decorations.markings}
\tikzset{rbox/.style= {draw=black,fill=white,thick,rectangle,rounded corners,inner xsep=2pt, inner ysep=2pt},
    fancytitle/.style={fill=blue!15,draw=black,thick,text=black,rectangle,rounded corners},}
\begin{document}
\begin{tikzpicture}[transform shape]
\node [rbox] (box) {
\begin{minipage}{.8\linewidth}
     { 
\begin{center}
\begin{tikzpicture}[line width=1pt,sharp corners,x=1cm,y=1cm,
                every node/.style={font=\footnotesize}]
\clip       (0,-0.5)            rectangle           (6,6);
\draw    [help lines]    (0,0)          grid        (6,6);
%   Axes
\draw   [<->,>=triangle 45]         (0,3)           --          (6,3);
\draw   [<->,>=triangle 45]         (3,0)           --          (3,6);
\draw   [decoration={ticks,amplitude=.75mm,segment length=5mm},decorate,line width=0.75pt]
                            (0.5,3)     --          (5.5,3);
\draw   [decoration={ticks,amplitude=.75mm,segment length=5mm},decorate,line width=0.75pt]
                            (3,0.5)     --          (3,5.5);
%   Line
\draw   [*-*]                   (0.5,0.5)       --          (5.5,5.5);
%   Counting
\draw       [->,line width=1pt,blue]                (0.5,0.5) .. controls (.65,0) and (.85,0) .. (1,0.5)
                                            .. controls (1.15,0) and (1.35,0) .. (1.5,0.5)
                                            .. controls (1.65,0) and (1.85,0) .. (2,0.5);
\end{tikzpicture} 
\end{center}
     }
\end{minipage}
};
\node[fancytitle,right=8pt] at (box.north west) {Grid:};
\end{tikzpicture}
\end{document}
3

Rather than giving control points, you can specify a curve using to and the options bend right=<angle> and looseness=<...>. I added the following to your code:

\draw [->,line width=1pt,red] (0.5,0.5) 
to[bend right=80,looseness=2.5] (1,0.5) 
to[bend right=80,looseness=2.5] (1.5,0.5) 
to[bend right=80,looseness=2.5] (2,0.5);

If you would like even more automation, you can get TikZ to calculate the sequence 1,1.5,2 of x-values from a given \startx value and \step between values, like so:

\def\startx{0.5}
\def\starty{0.5}
\def\step{0.5}
\def\finishx{2}
\pgfmathsetmacro\nextx{\startx+\step}
\pgfmathsetmacro\nextnextx{\startx+2*\step}
\draw [->,line width=1pt,red] (\startx,\starty) 
\foreach \x in {\nextx,\nextnextx,...,\finishx} {
  to[bend right=80,looseness=2.5] (\x,0.5)
};

In any case, the result is:

enter image description here


Update. I noticed that your black blobs aren't quite centred in their respective grid squares. That's because TikZ draws arrow tips so that the end of the arrow tip is the specified point. In your case, I presume that you want the centre of your arrow tip to be at the specified point. You could fix this by drawing the blobs as nodes, and then connecting them with a line, like so:

%\draw   [*-*] (0.5,0.5) -- (5.5,5.5);
\node[shape=circle,fill=black,outer sep=0mm] (a) at (0.5,0.5) {};
\node[shape=circle,fill=black,outer sep=0mm] (b) at (5.5,5.5) {};
\draw (a) -- (b);

Update 2. To get...

  • an additional sequence of arcs extending vertically from the same starting point, and
  • a numeric label on each arc

... you can use the following code.

\def\startx{0.5}
\def\starty{0.5}
\def\stepx{0.5}
\def\stepy{0.25}
\def\finishx{2}
\def\finishy{2.25}
\pgfmathsetmacro\nextx{\startx+\stepx}
\pgfmathsetmacro\nextnextx{\startx+2*\stepx}
\pgfmathsetmacro\nexty{\starty+\stepy}
\pgfmathsetmacro\nextnexty{\starty+2*\stepy}
\draw [->,line width=1pt,red] (\startx,\starty) 
\foreach [count=\i] \x in {\nextx,\nextnextx,...,\finishx} {
  to[bend right=80,looseness=2.5] node[green, auto, swap] {\tiny\i}  (\x,\starty)
};
\draw [->,line width=1pt,red] (\startx,\starty) 
\foreach [count=\i] \y in {\nexty,\nextnexty,...,\finishy} {
  to[bend left=80,looseness=2.5] node[green, auto] {\tiny\i} (\startx,\y)
};

enter image description here

| improve this answer | |
  • Is it possible to also include a count of each step as a label? – Calhistorian May 30 '13 at 10:03
  • Yes. Below each "valley", or above each "peak"? – John Wickerson May 30 '13 at 10:14
  • Below each valley. But also, how can I adjust the code to do the same for the y-axis. I tried manipulating the code but to no avail. – Calhistorian May 30 '13 at 10:16
  • By "the same for the y-axis", do you mean you want another sequence of arcs extending upwards from the same point? – John Wickerson May 30 '13 at 10:33
  • Yes. That's exactly what I mean. I was a bit vague. – Calhistorian May 30 '13 at 10:36

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.