Best practice: Lines in tikz with datavisualization

tikz-datavisualizationtikz-pgf

I plot a function with tikz using datavisualization. Whats the best/shortest possibility to realize some arrows inside, see the following output?

datavisualization with arrows

This code works:

\documentclass[crop,tikz]{standalone}% 'crop' is the default for v1.0, before it was 'preview'
\usetikzlibrary{datavisualization, datavisualization.formats.functions, shapes, calc, intersections, arrows, decorations.markings, plotmarks, decorations, tikzmark, angles, quotes, babel, patterns, positioning}
\begin{document}
\begin{tikzpicture}[]
        \datavisualization [school book axes, visualize as smooth line/.list={func1,func2}, visualize as line/.list={line1,line2,line3,line4,line5},all axes=grid, x axis={label=$x$, ticks={step=1}}, y axis={label=$y$, ticks={step=1}}, style sheet=strong colors, style sheet=vary dashing, func1={label in data={text'={$c=0$}, when=y is -3}}, func2={label in data={text={$c=3$}, when=x is -2}},line1={style={blue ,ultra thick, solid,{->}}, label in data={text'={\color{blue}$+3$}, when=y is -0.6}},line2={style={blue ,ultra thick, solid,{->}}, label in data={text'={\color{blue}$+3$}, when=y is 0.75}},line3={style={blue ,ultra thick, solid,{->}}, label in data={text'={\color{blue}$+3$}, when=y is 1.5}},line4={style={blue ,ultra thick, solid,{->}}, label in data={text'={\color{blue}$+3$}, when=y is 0.5}},line5={style={blue ,ultra thick, solid,{->}}, label in data={text'={\color{blue}$+3$}, when=y is -0.5}}]
        data[set = line1] {
            x, y
            -2, -2
            -2, -0.6
            -2, 1
        }
        data[set = line2] {
            x, y
            -1, -0.5
            -1, 0.75
            -1, 2.5
        }
        data[set = line3] {
            x, y
            0, 0
            0, 1.5
            0, 3
        }
        data[set = line4] {
            x, y
            1, -0.5
            1, 0.5
            1, 2.5
        }
        data[set = line5] {
            x, y
            2, -2
            2, -0.5
            2, 1
        }
        data [format=function, set=func1] {
            var x : interval [-2.5:2.5];
            func y = -0.5 * \value x * \value x;
        }
        data [format=function, set=func2] {
            var x : interval [-3.5:3.5];
            func y = -0.5 * \value x * \value x+3;
        };
    \end{tikzpicture} 
\end{document}

Here I there several problems: I have to define every arrow as single data set. That means to redefine the same style options many times. And the definition via data points seems cumbersome to me.

Any idea? Thanks a lot 🙂

Best Answer

I have never use datavisualisation, but i propose a solution not "very good" (it' better use the function but i don't know why) with foreach

            \documentclass[crop,tikz]{standalone}% 'crop' is the default for v1.0, before it was 'preview'
    \usetikzlibrary{datavisualization, datavisualization.formats.functions, shapes, calc, intersections, arrows, decorations.markings, plotmarks, decorations, tikzmark, angles, quotes, babel, patterns, positioning}
    \begin{document}
    \begin{tikzpicture}[]
        \datavisualization [
        school book axes,
        visualize as smooth line/.list={func1,func2},
        all axes=grid,
        x axis={label=$x$, ticks={step=1}}, y axis={label=$y$, ticks={step=1}}, style sheet=strong colors,
        style sheet=vary dashing,
        func1={label in data={text'={$c=0$}, when=y is -3}},
        func2={label in data={text={$c=3$}, when=x is -2}},
        ]
        data [format=function, set=func1] {
                var x : interval [-2.5:2.5];
                func y = -0.5 * \value x * \value x;
            }
        data [format=function, set=func2] {
                var x : interval [-3.5:3.5];
                func y = -0.5 * \value x * \value x+3;
            };

        \foreach \x in {-2,-1,...,2}
        \draw[blue,ultra thick, solid,{->}](\x,-0.5 * \x * \x)--node[midway,right]{$+3$}++(0,3);
    \end{tikzpicture}
    \end{document}

enter image description here

Related Question