4

normally i'm using plain tikz for curve plotting. I need "school book style" coordinate systems with a 50mm grid.

I tried using the tikz data visualization library.

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}[]
\datavisualization [ 
      school book axes={unit=0.5},
      visualize as smooth line, 
      x axis={label={$x$},grid,grid={minor steps between steps=1}}, 
      y axis={label={$y$},grid,grid={minor steps between steps=1}},
      every major grid/.style = {style={gray, thin}},
      every minor grid/.style = {style={gray, very thin}}
]

data [format=function] { 
  var x : interval [-2:2]; 
  func y = 1/2*(\value x)^2; 
}
info' {
   \fill[fill=lightgray] (visualization cs: x=1, y=0) -- plot [domain=1:2] (visualization cs: x=\x,y={0.5*(\x)^2}) -- (visualization cs: x=2, y=0) --cycle;
}
;
\end{tikzpicture}

\end{document}

The only thing i did not get right, is coloring the area under a curve. I tried the following in the info' block:

\fill[fill=lightgray] (visualization cs: x=1, y=0) -- plot [domain=1:2] (visualization cs: x=\x,y={0.5*(\x)^2}) -- (visualization cs: x=2, y=0) --cycle;

But this results in a PGF Math Error. Package PGF Math Error: Could not parse input '0.5*(1)^2'

I guess i need a way to tell the plot command to use the visualization cs.

Thanks in advance.

Cheers

4

Better create a new visualizer different from visualize as line. So I copy the definition of the latter from tikzlibrarydatavisualization.code.tex. The only different is that

every path/.style={draw},

is changed to

every path/.style={draw,fill},

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{datavisualization,datavisualization.formats.functions}

\begin{document}

\makeatletter
\tikzdatavisualizationset{
  visualize as pie/.style={
    new object={
      when=after survey,
      store=/tikz/data visualization/visualizers/#1,
      class=plot handler visualizer,
      arg1=#1,
      arg2={\tikz@dv@plot@handler,\tikz@dv@plot@mark@maker}
    },
    new visualizer={#1}{%
      every path/.style={draw,fill},
      style={every mark/.append style={color=visualizer color}},
      mark size=2pt,
      semithick,
      color=visualizer color,
      mark=none,
      /tikz/data visualization/every visualize as line/.try,
    }{visualizer in legend=\tikz@dv@legend@entry@as@example},
    #1={straight line}
  },
  visualize as pie/.default=pie,
}

\begin{tikzpicture}
    \datavisualization[
        school book axes={unit=0.5},
        x axis={label={$x$},grid,grid={minor steps between steps=1}}, 
        y axis={label={$y$},grid,grid={minor steps between steps=1}},
        every major grid/.style={style={gray,thin}},
        every minor grid/.style={style={gray,very thin}},
        visualize as pie
    ]
        data point[x=-2, y=0]
        data[format=function]{var x :interval [-2:2];func y =1/2*(\value x)^2;}
        data point[x=2, y=0];
\end{tikzpicture}

\end{document}
  • So i can overlay both visualizers to get a colored area from a to b. Thank you! Can you give me a hint where i can specify the fill color/pattern? – cw79 Feb 15 '15 at 17:19
  • I believe it's generally good practice to close your "hacks" with \makeatother. – Radon Rosborough Aug 1 '15 at 21:31
5

Not exactly using visualization but pgfplots with its fillbetween library. This will look easy.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{fillbetween}

\pagestyle{empty}
\begin{document}

\begin{tikzpicture}[]
  \begin{axis}[
    grid=both,
    ymin=0,
    xmin=-3,xmax=3,
    axis on top
    ]
    \addplot[draw=none,name path=A,domain=-2:2,fill=olive!40] {1/2*(x)^2}\closedcycle;
    \addplot[solid,thick,red,domain=-2:2] {1/2*(x)^2};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I have first filled the region and the plotted the curve so as to avoid the lines around the fill. Also, axis on top helps in keeping the x axis on top in this case.

  • I have rather rigid requirements for my plots. I need a 50mm grid in print, centered axes and the option to draw figures (e.g. triangles, rectancles, circles) onto the coordinate system. – cw79 Feb 15 '15 at 14:03
  • @cw79 All those can be done within this frame work. :-) – user11232 Feb 15 '15 at 14:05
0

Using coordinate calculation (\usetikzlibrary{calc}) one can do a coordinate transformation. Unfortunately, I only found this kind of messy transformation, maybe someone can post a cleaner way to do this.

info' {
  \begin{scope}[shift={(visualization cs:x=0, y=0)},
     x={($(visualization cs:x=1, y=0)-(visualization cs:x=0, y=0)$)},
     y={($(visualization cs:x=0, y=1)-(visualization cs:x=0, y=0)$)}]
    \fill[fill=lightgray] (1,0) -- plot [domain=1:2] (\x,{0.5*(\x)^2}) -- (2, 0) --cycle;
  \end{scope}
}

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.