4

I have this code

    \begin{tikzpicture}
    \def\a{5}
    \draw[->] (-\a,0) -- (+\a,0);
    \draw[->] (0,-\a) -- (0,+\a);
    \draw[thick, red,  samples=100, domain=-.7:3.2] plot ({\x},{ \x*\x*(1-\x)*(3-\x) });   
    \def\E{2}
    \draw[thick, green] (-\a,\E) -- (+\a,\E) node[xshift=0.5cm] {$ E $};
    \end{tikzpicture}

and I have this output

enter image description here

How to limit the codomain range?

6

There is a dedicated plotting package built on tikz namely, pgfplots. One implementation of your plot in that could be:

Sample output

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[domain=-.7:3.2, ymin=-1, ymax=1,
    axis x line=middle, axis y line=middle, ticks=none,
    enlarge x limits={rel=0.07}]
    \addplot[thick, red, samples=100] {(x*x*(1-x)*(3-x))};
    \addplot[green] {(.5)} node[right]{$E$};
  \end{axis}
\end{tikzpicture}

\end{document}

You can move the codomain restrictions to just the one function, but putting them as options to the \addplot command, but you have to use the option

restrict y to domain=-1:1

and to get the cut-off right in your example you then have to increase the number of samples:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[domain=-.7:3.2,
    axis x line=middle, axis y line=middle, ticks=none,
    enlarge x limits={rel=0.07}]
    \addplot[thick, red, samples=5000, restrict y to domain=-1:1] {(x*x*(1-x)*(3-x))};
    \addplot[green] {(.5)} node[right]{$E$};
  \end{axis}
\end{tikzpicture}

\end{document}

If you want to control the axis lengths precisely, use xmin/xmax/ymin/ymax and adjust the domains on the individual plots:

Second sample

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[xmin=-5, xmax=5, ymin=-1.2, ymax=1.2,
    axis x line=middle, axis y line=middle, ticks=none]
    \addplot[thick, red, samples=5000,
      domain=-.7:3.2, restrict y to domain=-1:1] {(x*x*(1-x)*(3-x))};
    \addplot[domain=-5:4.2, green] {(.5)} node[right]{$E$};
  \end{axis}
\end{tikzpicture}

\end{document}
11
  • restrict y to domain=a:b is probably more what the OP wants. – Torbjørn T. Oct 19 '17 at 8:53
  • @TorbjørnT. I used that initially, but found it was clipping at the wrong places, perhaps because it requires more samples... – Andrew Swann Oct 19 '17 at 8:56
  • True, that can be a problem. – Torbjørn T. Oct 19 '17 at 8:58
  • So there is no way to restric only the plot of the function? – Andrea Leo Oct 19 '17 at 9:12
  • Yes, just put the options (either restrict y to domain or ymin/ymax) in the \addplot command rather than the axis, if that is what you want. – Andrew Swann Oct 19 '17 at 10:13
3

\clip? The purpose of the scope environment is to localize the effect of the \clip.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \def\a{5}
  \def\E{2}
  \draw[->] (-\a,0) -- (+\a,0);
  \draw[->] (0,-\a) -- (0,+\a);
  \begin{scope}
    \clip (-\a,-\a) rectangle (\a,\E);
    \draw[thick, red,  samples=100, domain=-.7:3.2] plot ({\x},{ \x*\x*(1-\x)*(3-\x) });
  \end{scope}
  \draw[thick, green] (-\a,\E) -- (+\a,\E) node[xshift=0.5cm] {$ E $};
\end{tikzpicture}
\end{document}
3
  • Thank you but I prefer some solution like \draw[thick, red, samples=100, domain=-.7:3.2, codomain = somerange: somerange] – Andrea Leo Oct 19 '17 at 8:41
  • @AndreaLeo pgfplots (see Andrew's answer) and its restrict y to domain key perhaps. I don't know at the moment how to do that, though others might come up with something. – Torbjørn T. Oct 19 '17 at 8:56
  • @AndreaLeo You could always wrap the scope, \clip and \draw into one \newcommand, if you must do this in one line of code. – Torbjørn T. Oct 19 '17 at 10:09

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.