0

I wanted to create a sinusoidal waveform using pgfplots, which "clips" the top wave. I am attaching the code that I tried to do, but the wave form becomes smooth and offset from the node.

\begin{tikzpicture}
\draw[very thick,->](0,0)to++(4,0)node[below right]{$t$};
\draw[very thick,->](0,0)to++(0,4)node[below left]{$V_{out}$};
\draw[magenta, dashed](0,3)node(vout){}node[left]{$V_{out1,CM}$}to++(4,0);
%\draw[cyan, dashed](0,3)node[left]{$V_{out2,CM}$}to++(4,0);
\begin{axis}[
        width=2.75cm, height=2.5cm,
        trig format plots=rad,
        enlarge x limits=false,
        xtick=\empty,
        axis lines*=middle,
        hide y axis,
        hide x axis,
        scale=3,        
%        axis x line=bottom,
        at={(vout.west)},
        anchor=west,
        xshift=3mm,
         xlabel=$t$,
        every axis x label/.style={
            at={(ticklabel* cs:1)},
            anchor=west,
        },
        ]

    \addplot [no markers,smooth, domain=0:6*pi, magenta] {(-sin(\x)>=0.5)?0.5:-sin(\x)};
%    \addplot [no markers,smooth, domain=0:6*pi, magenta] {(-sin(\x)>=0.5)?\empty:-sin(\x)};
%    \draw[dashed,black!80] (axis cs: 0,0) -- (axis cs: 2*pi,0);
\end{axis}
\end{tikzpicture}

and the corresponding output shown below:

enter image description here

but I need a graph like this:

enter image description here

1 Answer 1

1

You were absolutely on the right track. But you just used too few sample points which in combination with smooth caused the "overshooting". Thus, increasing samples (from default 25) to 201 yields the desired result.

(Besides that I simplified your code a bit ...)

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=left,
        xlabel=$t$,
        ylabel=$V_{out}$,
        every axis x label/.style={
            at={(ticklabel* cs:1)},
            anchor=north west,
        },
        every axis y label/.style={
            at={(ticklabel* cs:1)},
            anchor=north east,
        },
        ymin=-1.5,
        ymax=1,
        trig format plots=rad,
        enlarge x limits=false,
        xtick=\empty,
        ytick=\empty,
        clip=false,
        % moved common `\addplot` options here
        no markers,
        smooth,
        domain=0:6*pi,
        % increased number of samples to reduce "overshooting" caused from `smooth`
        samples=201,
    ]

%        \addplot [dashed,samples=2] {0.5};
        \addplot [dashed,samples=2,magenta] {0}
            node [at start,left] {$V_{out1,CM }$}
        ;
        \addplot+ [magenta] {(-sin(x)>=0.5)?0.5:-sin(x)};
        % (a bit simpler way to do what you want)
        \addplot+ [blue,dashed,restrict y to domain*=-1:0.5] {-sin(x)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

3
  • Thank you, but I have one doubt. How to shift the entire graph in y-axis. And what does {0} signifies in this "\addplot [dashed,samples=2,magenta] {0} node [at start,left] {$V_{out1,CM }$}". Is it pointing to the origin of the wave?
    – Raja
    Jan 29, 2019 at 16:26
  • 1
    Maybe first to the second question: When one (only) writes \addplot {...} then the part in the curly bracket is expected a (mathematical) expression, that in this case is y=0. Thus a horizontal line at y=0 is drawn. If you need the horizontal line higher or lower, just change the value. Jan 29, 2019 at 16:44
  • 1
    To the first question: That depends on what you exactly mean by "shifting". E.g. you could add xmin=-1 to the axis options to draw the y axis at x=-1 instead of presently x=0. Or you can change the domain given to the axis options to 0.5:6*pi and add domain=0:6*pi to the options of the first \addplot command. Jan 29, 2019 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .