2

I would like to plot an hysteresis of a Schmitt-Trigger. The following figure shows the desired graph.

Schmitt-Trigger Hysteresis

How can I achive this using pgfplots? I have the minimum and maximum of both axis values as known dimensions.

Edit: I came up with something like this:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
    [ylabel={$U_y [V]$},
    xlabel={$U_x [V]$},
    ymin=-10,ymax=10,
    xmin=-10,xmax=10,
    minor y tick num=1,
    grid=major,
    legend entries={{$R_1 = R_2 = 470 k\Omega$}
    ,{$R_1 = 1 k\Omega, R_2 = 4,7 k\Omega$},
    {$R_1 = 470 \Omega, R_2 = 10 k\Omega$}},
    legend pos=outer north east]
\addplot[mark=none,red] coordinates {(-8,-8) (4,-8) (4,8) (8,8) (-4,8) (-4,-8)};
\addplot[mark=none,brown] coordinates {(-8,-8) (1.4,-8) (1.4,8) (8,8) (-1.4,8) (-1.4,-8)};
\addplot[mark=none,blue] coordinates {(-8,-8) (0.3,-8) (0.3,8) (8,8) (-0.3,8) (-0.3,-8)};
\end{axis}
\end{tikzpicture}

\end{document}

latex preview of plot

3
  • 3
    Have you made any attempt so far? If so please post whatever code you have. – Null Aug 4 '16 at 5:35
  • 1
    Drawing this in pure tikz is more simple than in pgfplots. – Zarko Aug 4 '16 at 5:53
  • @Null Added my attempts, no arrows so far and I wonder if transparancy could help in displaying the different plots more clearly. – maxik Aug 4 '16 at 8:19
4

You can investigate with pgfplots marks, but decorations.markings can serve to draw arrows.

What I've done is keeping your pgfplots draw but adding a draw command which will repeat lines but adding arrow decorations on them. I've tried adding decorations to \addplot commands but they also apply to lines in legend.

\documentclass[border=2mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings, arrows.meta}
\pgfplotsset{compat=1.13}

\tikzset{myarrows/.style={
    decorate,
    decoration={markings,
        mark=at position .04 with {\arrowreversed{Latex}},
        mark=at position .1 with {\arrow{Latex}},
        mark=at position .2 with {\arrow{Latex}},
        mark=at position .6 with {\arrow{Latex}}
    }}}

\begin{document}

\begin{tikzpicture}[]
\begin{axis}
    [ylabel={$U_y [V]$},
    xlabel={$U_x [V]$},
    ymin=-10,ymax=10,
    xmin=-10,xmax=10,
    minor y tick num=1,
    grid=major,
    legend entries={{$R_1 = R_2 = 470 k\Omega$}
    ,{$R_1 = 1 k\Omega, R_2 = 4,7 k\Omega$},
    {$R_1 = 470 \Omega, R_2 = 10 k\Omega$}},
    legend pos=outer north east]
\addplot[mark=none,red] coordinates {(-8,-8) (4,-8) (4,8) (8,8) (-4,8) (-4,-8)};
\addplot[mark=none,brown] coordinates {(-8,-8) (1.4,-8) (1.4,8) (8,8) (-1.4,8) (-1.4,-8)};
\addplot[mark=none,blue] coordinates {(-8,-8) (0.3,-8) (0.3,8) (8,8) (-0.3,8) (-0.3,-8)};

\draw[red,myarrows](-8,-8)-|(4,8);
\draw[red,myarrows] (8,8)-|(-4,-8);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Update:

I've found an answer from Christian Feuersänger to Tikz path with changing color and arrow decorations which can be used to draw arrows with a addplot command. It uses markings decoration with a list of points where to place the arrow tips with options to fix kind of arrow, color, scale, ...

\documentclass[border=2mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings, arrows.meta}
\pgfplotsset{compat=1.13}

\tikzset{
  set arrow inside/.code={\pgfqkeys{/tikz/arrow inside}{#1}},
  set arrow inside={end/.initial=>, opt/.initial=},
  /pgf/decoration/Mark/.style={
    mark/.expanded=at position #1 with
    {
      \noexpand\arrow[\pgfkeysvalueof{/tikz/arrow inside/opt}]{\pgfkeysvalueof{/tikz/arrow inside/end}}
    }
  },
  arrow inside/.style 2 args={
    set arrow inside={#1},
    postaction={
      decorate,decoration={
        markings,Mark/.list={#2}
      }
    }
  },
}

\begin{document}

\begin{tikzpicture}[]
\begin{axis}
    [ylabel={$U_y [V]$},
    xlabel={$U_x [V]$},
    ymin=-10,ymax=10,
    xmin=-10,xmax=10,
    minor y tick num=1,
    grid=major,
    legend entries={{$R_1 = R_2 = 470 k\Omega$}
    ,{$R_1 = 1 k\Omega, R_2 = 4,7 k\Omega$},
    {$R_1 = 470 \Omega, R_2 = 10 k\Omega$}},
    legend pos=outer north east]
\addplot[mark=none,brown] coordinates {(-8,-8) (1.4,-8) (1.4,8) (8,8) (-1.4,8) (-1.4,-8) (-8,-8)};
\addplot[mark=none,blue] coordinates {(-8,-8) (0.3,-8) (0.3,8) (8,8) (-0.3,8) (-0.3,-8) (-8,-8)};
\addplot[mark=none,red] coordinates {(-8,-8) (4,-8) (4,8) (8,8) (-4,8) (-4,-8) (-8,-8)};

\addplot[draw=none,red] coordinates {(-8,-8) (4,-8) (4,8) (8,8) (-4,8) (-4,-8) (-8,-8)} [arrow inside={end=Latex, opt={red, scale=1.5}}{0.04,0.25,0.49,0.54,0.75,0.99}];
\addplot[draw=none,brown] coordinates {(-8,-8) (1.4,-8) (1.4,8) (8,8) (-1.4,8) (-1.4,-8) (-8,-8)} [arrow inside={end=Latex, opt={brown, scale=1.5}}{0.22,0.72}];
\addplot[draw=none,blue] coordinates {(-8,-8) (0.3,-8) (0.3,8) (8,8) (-0.3,8) (-0.3,-8) (-8,-8)} [arrow inside={end=Latex, opt={blue, scale=1.5}}{0.22,0.72}];

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • That looks not that bad, but colored arrows would be better I think. Anyway, nice solution. – maxik Aug 4 '16 at 16:38
  • @maxik I've updated the answer with a solution better integrated with pgfplots and easily customizable arrows. Hope it helps. – Ignasi Aug 5 '16 at 8:52
  • Well done! I will add some transparency and look if this helps the appearence. – maxik Aug 5 '16 at 9:08

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.