3

MWE:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{arrows,calc}
\usetikzlibrary{shapes,positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{datavisualization.formats.functions}
\def\mytypesetter#1{
\pgfmathparse{#1/pi}
\pgfkeys{/pgf/number format/precision=2}
\pgfmathroundtozerofill{\pgfmathresult}
\pgfmathifisint{\pgfmathresult}{
\pgfmathprintnumber{\pgfmathresult}$\pi$
}{
\pgfmathprintnumber[/pgf/number format/frac, frac denom=2, frac whole=false]{\pgfmathresult}$\pi$
}
}
\begin{document}


\begin{tikzpicture}[>=latex]
\datavisualization [
 school book axes, %scientific axes=clean,
  all axes={},
  x axis={
    label=$\omega t$,
    ticks={
      step=(pi/2),
      tick typesetter/.code=\mytypesetter{##1},}
  },
  y axis={
    label=$v$, 
    ticks=none,
  },
  style sheet=strong colors,
  style sheet=vary dashing,
  visualize as smooth line/.list={voltage},
  voltage={label in legend={text={$v$}}},
]
data [set=voltage, format=function] {
  var x : interval [0:2*pi] samples 100;
  func y = 2*sin(\value x r);
};
\end{tikzpicture} 
\end{document}

But this is what I want

enter image description here.

How do I get this graph from my source code without using axis environment?

  • @marmot, I had seen but could not adapt to my source code. – Özgür Mar 24 at 1:27
  • I quickly tried and could at least shade the curve. Most likely one can also draw the vertical lines. However, why do you not want to use JouleV's nice solution or pgfplots? – marmot Mar 24 at 1:30
  • It's a really nice solution. But I've got 50+ pages of graphics. If I can't find the right solution, of course I will. – Özgür Mar 24 at 1:36
4

Nothing prevents you from adding the fills on the background.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\usetikzlibrary{patterns,backgrounds}
\def\mytypesetter#1{
\pgfmathparse{#1/pi}
\pgfkeys{/pgf/number format/precision=2}
\pgfmathroundtozerofill{\pgfmathresult}
\pgfmathifisint{\pgfmathresult}{
\pgfmathprintnumber{\pgfmathresult}$\pi$
}{
\pgfmathprintnumber[/pgf/number format/frac, frac denom=2, frac whole=false]{\pgfmathresult}$\pi$
}
}
\begin{document}


\begin{tikzpicture}[>=latex]
\datavisualization [
 school book axes, %scientific axes=clean,
  all axes={},
  x axis={
    label=$\omega t$,
    ticks={
      step=(pi/2),
      tick typesetter/.code=\mytypesetter{##1},}
  },
  y axis={
    label=$v$, 
    ticks=none,
  },
  style sheet=strong colors,
  style sheet=vary dashing,
  visualize as smooth line/.list={voltage},
  voltage={label in legend={text={$v$}}},
]
data [set=voltage, format=function] {
  var x : interval [0:2*pi] samples 100;
  func y = 2*sin(\value x r);
};
\begin{scope}[on background layer]
 \fill[red] plot[variable=\x,domain=0:pi] ({\x},{2*sin(\x r)});
 \path[pattern=horizontal lines,pattern color=red] 
 plot[variable=\x,domain=pi:2*pi] ({\x},{2*sin(\x r)});
\end{scope} 
\end{tikzpicture} 
\end{document}

enter image description here

This lets you keep what you had and add the fills where needed.


Let me also comment that Symbol 1's nice answer can be used here. I stress that I have close to zero knowledge on the data visualization business, so I may make things unnecessarily complicated. The strategy is from Symbol 1's nice answer, except that I allow the style to be redefined via \tikzset{change style={draw,pattern=horizontal lines,pattern color=red}} (yes, I know that is a stupid name for a pgf key, yet I feel that this is not the only thing that I am doing stupid here. This works, but is far more complicated than JouleV's nice code. There is a good chance that (at least) part of the complexity is due to my ignorance with regards to data visualization.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{patterns}
\usetikzlibrary{datavisualization.formats.functions}
\def\mytypesetter#1{
\pgfmathparse{#1/pi}
\pgfkeys{/pgf/number format/precision=2}
\pgfmathroundtozerofill{\pgfmathresult}
\pgfmathifisint{\pgfmathresult}{
\pgfmathprintnumber{\pgfmathresult}$\pi$
}{
\pgfmathprintnumber[/pgf/number format/frac, frac denom=2, frac whole=false]{\pgfmathresult}$\pi$
}
}
\makeatletter%from https://tex.stackexchange.com/a/228265/121799
\tikzdatavisualizationset{
  visualize as generic/.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=generic style,
      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 generic/.default=generic,
}
\tikzset{change style/.code={\tikzset{generic style/.style={#1}}},
change style={draw,fill=red}}
\makeatother
\begin{document}


\begin{tikzpicture}[>=latex]
\datavisualization [
 school book axes, %scientific axes=clean,
  all axes={},
  x axis={include value=6.3,
    label=$\omega t$,
    ticks={
      step=(pi/2),
      tick typesetter/.code=\mytypesetter{##1},}
  },
  y axis={include value=-2,
    label=$v$, 
    ticks=none,
  },
  style sheet=strong colors,
  style sheet=vary dashing,
  visualize as generic,
  %visualize as smooth line/.list={voltage},
%  voltage={label in legend={text={$v$}}},
]
data [format=function] { % set=voltage,
  var x : interval [0:pi] samples 100;
  func y = 2*sin(\value x r);
} ;
\tikzset{change style={draw,pattern=horizontal lines,pattern color=red}}
\datavisualization [
 school book axes, %scientific axes=clean,
  all axes={},
  x axis={include value=6.3,
    label=$\omega t$,
    ticks={
      step=(pi/2),
      tick typesetter/.code=\mytypesetter{##1},}
  },
  y axis={include value=2,
    label=$v$, 
    ticks=none,
  },
  style sheet=strong colors,
  style sheet=vary dashing,
  visualize as generic,
  %visualize as smooth line/.list={voltage},
%  voltage={label in legend={text={$v$}}},
]
data [format=function]{
  var x : interval [pi:2*pi] samples 100;
  func y = 2*sin(\value x r);
};
\end{tikzpicture} 
\end{document}

enter image description here

  • It's working... – Özgür Mar 24 at 2:14
  • @Özgür I added a simple possibility that allows you to keep your 50+ plots and add fills where needed. – marmot Mar 24 at 3:07
  • I have a small suggestion: the red color seems to overfill the ωt axis a bit. – user156344 Mar 24 at 16:02
  • @JouleV Yes, that's true, thanks! One has to use the same number of samples to get a perfect match. – marmot Mar 24 at 17:28
7

A pure TikZ solution for fun, which is much shorter. I have no idea why you have to use \datavisualization for this.

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[>=latex,y=2cm]
\draw[smooth,fill=red] plot[samples=100,domain=0:pi] (\x,{sin(deg(\x))});
\draw[smooth,pattern=horizontal lines,pattern color=red] plot[samples=100,domain=pi:2*pi] (\x,{sin(deg(\x))});
\draw[->] (0,-1.1)--(0,1.1) node[above] {$v$};
\draw[->] (-.2,0)--(2*pi+0.5,0) node[right] {$\omega t$};
\draw (pi/2,-.05) node[below] {$\frac12\pi$}--(pi/2,.05);
\draw (pi,-.05) node[below] {$1\pi$}--(pi,.05);
\draw (3*pi/2,-.05) node[below] {$\frac32\pi$}--(3*pi/2,.05);
\draw (2*pi,-.05) node[below] {$2\pi$}--(2*pi,.05);
\draw (8.5,0) node {\tikz\draw[x=1pt,y=1pt] (0,0) to[out=30,in=-150] (20,0); $v$};
\end{tikzpicture}
\end{document}

enter image description here

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.